Good morning,
Yesterday I was searching the internet a code in which I could place a button inside a table. I found the code below, I am usually able to put the button and made some adjustments to my system, but I'm not able to recover the action of clicking it, can someone help me?
class BotaoVisualizarTabelaProgramadas extends AbstractCellEditor implements TableCellRenderer, TableCellEditor, ActionListener { JTable table; JButton renderButton; JButton editButton; String text; public BotaoVisualizarTabelaProgramadas(JTable table, int column) { super(); this.table = table; renderButton = new JButton(); editButton = new JButton(); editButton.setFocusPainted(false); editButton.addActionListener(this); TableColumnModel columnModel = table.getColumnModel(); columnModel.getColumn(column).setCellRenderer(this); columnModel.getColumn(column).setCellEditor(this); } @Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (hasFocus) { renderButton.setForeground(table.getForeground()); renderButton.setBackground(UIManager.getColor("Button.background")); } else if (isSelected) { renderButton.setForeground(table.getSelectionForeground()); renderButton.setBackground(table.getSelectionBackground()); } else { renderButton.setForeground(table.getForeground()); renderButton.setBackground(UIManager.getColor("Button.background")); } renderButton.setText((value == null) ? "" : value.toString()); return renderButton; } @Override public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column) { text = (value == null) ? "" : value.toString(); return editButton; } @Override public Object getCellEditorValue() { return text; } @Override public void actionPerformed(ActionEvent e) { fireEditingStopped(); System.out.println(e.getActionCommand() + " : " + table.getSelectedRow()); } }