3

I got the problem, that I cannot click on buttons. They behave like they are just textfields with the design of buttons.

my Main:

    tableModStudents = (DefaultTableModel) studentsTable.getModel();
    studentsTable.getColumn(studentsTable.getColumnName(8))
                 .setCellRenderer(new JButtonRenderer());
    studentsTable.getColumn(studentsTable.getColumnName(8))
                 .setCellEditor(new JButtonEditor());

my CellRenderer:

public class JButtonRenderer implements TableCellRenderer {    
    private JButton button = new JButton();

    public Component getTableCellRendererComponent(JTable table,
            Object buttonText, boolean isSelected, boolean hasFocus, 
            int row, int column) {
        table.setShowGrid(true);
        button.setText("Details");
        button.setToolTipText(buttonText.toString());
        return button;
    }
}

my CellEditor:

    public class JButtonEditor extends AbstractCellEditor implements TableCellEditor {

    private JButton button;
    private String txt;

    public JButtonEditor() {
        super();
        button = new JButton();
        button.setOpaque(true);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                System.out.println("Button gedrückt!");
            }
        });
    }

    public Object getCellEditorValue() {
        return null;
    }

    public boolean isCellEditable(EventObject anEvent) {
        return true;
    }

    public boolean shouldSelectCell(EventObject anEvent) {
        return false;
    }

    public boolean stopCellEditing() {
        return super.stopCellEditing();
    }

    public void cancelCellEditing() {
    }

    public void addCellEditorListener(CellEditorListener l) {
    }

    public void removeCellEditorListener(CellEditorListener l) {
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        txt = (value == null) ? "" : value.toString();
        button.setText(txt);
        return button;
    }
}

Can you find the issue with that? It drives my crazy...

Thanks so much :)

yyunikov
  • 5,719
  • 2
  • 43
  • 78
  • This [question](http://stackoverflow.com/q/1475543/243373) on SO has some links to solutions. – TT. Nov 05 '16 at 13:46

1 Answers1

5

Check out Table Button Column.

It combines a button renderer and editor in a single class.

All you need to do is provide the custom Action to be invoked when you invoke the button (either by clicking on it or by invoking its mnemonic).

camickr
  • 321,443
  • 19
  • 166
  • 288