Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding button to jtable

I searched for tutorials for adding button in jtable and found a class file from, http://tips4java.wordpress.com/2009/07/12/table-button-column/ Where to set label for the button?

      [code]
   private void createTable(){
       model = new DefaultTableModel();
       editorTable.setModel(model);
       model.addColumn("COL1");
       model.addColumn("COL2");
       model.addColumn("ADD");
       model.addColumn("DELETE");
       model.addRow(new Object[]{"DATA1", "DATA2"});

       Action delete = new AbstractAction() {

       @Override
       public void actionPerformed(ActionEvent e) {
           editorTable = (JTable) e.getSource();
           int modelRow = Integer.valueOf(e.getActionCommand());
           ((DefaultTableModel) editorTable.getModel()).removeRow(modelRow);
       }
   };

         ButtonColumn bc = new ButtonColumn(editorTable, delete, 3);
         bc.setMnemonic(KeyEvent.VK_D);
  }

     [/code]
like image 493
FirmView Avatar asked Sep 18 '26 18:09

FirmView


1 Answers

It is set automatically in the table renderer and editor from the data in your DefaultTableModel. For example, for the table editor, the code is:

public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column) {
  ...
  editButton.setText( value.toString() );
  editButton.setIcon( null );
  ...
}

where value is the value from your table model. See ButtonColumn.java for details.

EDIT: Since you are adding 4 columns, you should probably change your row data to

model.addRow(new Object[]{"DATA1", "DATA2", "DATA3", "DELETE"});

in order to see the delete buttons on the 4th column.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!