3

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]
mKorbel
  • 109,525
  • 20
  • 134
  • 319
FirmView
  • 3,130
  • 8
  • 34
  • 50
  • take a look here: http://stackoverflow.com/questions/1475543/how-to-add-button-in-a-row-of-jtable-in-swing-java – MByD Feb 17 '12 at 01:21
  • did you check oracle java tutorial on JTable : http://docs.oracle.com/javase/tutorial/uiswing/components/table.html there check on Renderers and Editors – asela38 Feb 17 '12 at 01:23
  • Binyamin, Thanks for responding, i implemented the class, it is working fine, but i am not finding where to add the label for the button. – FirmView Feb 17 '12 at 01:28

2 Answers2

4

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.

Kavka
  • 4,191
  • 16
  • 33
  • Thanks, Kaka, i saw the code, it is editorValue. If i want the name of the label to be DELETE, where should i have change? – FirmView Feb 17 '12 at 01:46
  • 1
    +1 for understanding the blog entry :) As the blog says `You store text (or an Icon) in the TableModel and the ButtonColumn will render the text (or the Icon) on a button`. So you add a text string to the TableModel the same way you add other text strings to the model, only the rendering is different. – camickr Feb 17 '12 at 02:17
  • Potential problem with the code in the blog entry : it doesn't work for more than one "button column". It only allows to pick one column that will be rendered as button. – Pierre Henry Oct 05 '15 at 15:21
-1
    MyClass myClass = new MyClass();
    jTable1.getColumnModel().getColumn(0).setCellEditor(myClass);
    jTable1.getColumnModel().getColumn(0).setCellRenderer(myClass);
class MyClass extends AbstractCellEditor implements TableCellEditor, TableCellRenderer
{

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
    {
    JPanel panel=(JPanel)jTable1.getCellRenderer(row, column).getTableCellRendererComponent(table, value, isSelected, isSelected, row, column);
    panel.setBackground(table.getSelectionBackground());    
    return panel;
    }

    @Override
    public Object getCellEditorValue()
    {
        return null;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        AbstractAction action = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JOptionPane.showMessageDialog(rootPane,"Row :"+jTable1.getSelectedRow()+"    "+ e.getActionCommand() + " clicked");
            }
        };
        JButton button1 = new JButton(action);
        JButton button2 = new JButton(action);
        button1.setText("Button1");
        button2.setText("Button2");

        JPanel panel = new JPanel();
        panel.add(button1);
        panel.add(button2);
        panel.setBackground(table.getBackground());
        return panel;
    }
}

}

Mulayam
  • 31
  • 3
  • multiple issues with this, f.i. a) cellEditor invalid implementation: it _must_ notify its listeners on termination b) creating new components all the time c) assuming the default renderers to be of type JPanel d) ... – kleopatra Aug 12 '13 at 09:42