Skip to content
Advertisement

Adding Buttons inside cell of JTable along with data?

Is it possible to add buttons inside the JTable cell along with data? What I am trying to do is to create a table with columns which display data(number) from the database, and two buttons to increase/decrease the number inside the same cell.

|ID | Quantity|
|06| 2 [+][-] |

it would be something like above with [+][-] being buttons. So when I press [+], the number will change to 3 and 1 if pressing [-].

Advertisement

Answer

Yes, it is possible, although It won’t be easy.

You have to write your own custom cell renderer and your own cell editor.

This is a sample I made in 5 minutes:

sample

It is far from perfect, but shows the concept.

Here’s the source code:

import java.awt.Component;
import java.awt.Font;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.Dimension;

public class CustomCell {
    public static void main( String [] args ) { 
        Object [] columnNames = new Object[]{ "Id", "Quantity" };
        Object [][] data        = new Object[][]{ {"06", 1}, {"08", 2} };

        JTable table = new JTable( data, columnNames ) { 
            public TableCellRenderer getCellRenderer( int row, int column ) {
                return new PlusMinusCellRenderer();
            }
         };

        table.setRowHeight( 32 );
        showFrame( table );
    }

    private static void showFrame( JTable table ) {
        JFrame f = new JFrame("Custom Cell Renderer sample" );
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        f.add( new JScrollPane( table ) );
        f.pack();
        f.setVisible( true );
    }
}

class PlusMinusCellRenderer extends JPanel implements TableCellRenderer {
        public Component getTableCellRendererComponent(
                            final JTable table, Object value,
                            boolean isSelected, boolean hasFocus,
                            int row, int column) {
                this.add( new JTextField( value.toString()  ) );
                this.add( new JButton("+"));
                this.add( new JButton("-"));
                return this;
        }
}

Here’s a thread that may be interesting and here.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement