Boa noite, gostaria de transformar a String de título de uma coluna em um botão
Tenho uma tabela:
tablepaciente.setModel(new DefaultTableModel(
new Object[][] {
{null, null, null, null, null, null},
{null, null, null, null, null, Boolean.FALSE},
{null, null, null, null, null, Boolean.FALSE},
{null, null, null, null, null, Boolean.FALSE},
{null, null, null, null, null, null},
{null, null, null, null, null, Boolean.FALSE},
{null, null, null, null, null, Boolean.FALSE},
{null, null, null, null, null, Boolean.FALSE},
{null, null, null, null, null, Boolean.FALSE},
{null, null, null, null, null, null},
},
new String[] {
"Nome", "CPF", "Data de Nascimento", "Profiss\u00E3o", "Editar", "Excluir"
}
) {
Class[] columnTypes = new Class[] {
Object.class, Object.class, Object.class, Object.class, Object.class, Boolean.class
};
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
});
Gostaria que a String "Excluir" seja um botão, e que a coluna de Editar seja uma coluna de botões.
Ja tentei ultilizar uma classe extendida AbstractCellEditor
import java.awt.*;
class ButtonColumn extends AbstractCellEditor implements TableCellRenderer, TableCellEditor{
JTable table;
JButton renderButton, editButton;
String text;
TableColumnModel columnModel;
public ButtonColumn(JTable table, int column){
super();
this.table = table;
renderButton = new JButton();
editButton = new JButton();
editButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TelaPaciente tp = new TelaPaciente();
tp.pconsultar.setVisible(false);
tp.cadastrarPaciente.setVisible(false);
tp.scrollPane.setVisible(false);
tp.validate();
}});
editButton.setFocusPainted( false );
editButton.setBackground(new Color(255, 255, 255));
columnModel = table.getColumnModel();
columnModel.getColumn(column).setCellRenderer( this );
columnModel.getColumn(column).setCellEditor( this );
}
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;
}
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column)
{
text = (value == null) ? "" : value.toString();
editButton.setText( text );
return editButton;
}
public Object getCellEditorValue()
{
return text;
}
}
na minha tabela eu estancio esta classe e aponto sua posição no array da tabela:
Ex:
editar = new ButtonColumn(tablepaciente, 4);
O problema é que não consigo fazer eventos ultilizando esta maneira, mesmo implementando o ActionListener nesta classe, o evento não funciona!!!
Se alguem tiver uma solução, por gentileza me auxiliem!!