Adding Buttons inside cell of JTable along with data?

后端 未结 3 1962
轻奢々
轻奢々 2020-12-06 17:50

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,

3条回答
  •  醉梦人生
    2020-12-06 18:31

    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.

提交回复
热议问题