Hola,
Primero debes implementar un TableCellRenderer, encargado de dibujar el botón.
Código:
public class myrenderer extends JLabel implements TableCellRenderer {
boolean isBordered = true;
public myrenderer(boolean isBordered) {
this.isBordered = isBordered;
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object color, boolean isSelected, boolean hasFocus, int row, int column) {
// Va a mostrar el botón solo en la última fila.
// de otra forma muestra un espacio en blanco.
if (row == table.getModel().getRowCount() - 1) {
return new JButton("Agregar");
} else {
setBackground(new Color(0xffffff));
return this;
}
}
}
Después tienes que implementar un TableCellEditor, encargado de las operaciones de edición, (activación del botón).
Código:
public class myeditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
Boolean currentValue;
JButton button;
protected static final String EDIT = "edit";
private JTable jTable1;
public myeditor(JTable jTable1) {
button = new JButton();
button.setActionCommand(EDIT);
button.addActionListener(this);
button.setBorderPainted(false);
this.jTable1 = jTable1;
}
public void actionPerformed(ActionEvent e) {
// mymodel t = (mymodel) jTable1.getModel();
// t.addNewRecord();
fireEditingStopped();
}
//Implement the one CellEditor method that AbstractCellEditor doesn't.
public Object getCellEditorValue() {
return currentValue;
}
//Implement the one method defined by TableCellEditor.
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
// Va a mostrar el botón solo en la última fila.
// de otra forma muestra un espacio en blanco.
if (row == table.getModel().getRowCount() - 1) {
currentValue = (Boolean) value;
return button;
}
return new JLabel();
}
}
Y después tienes que modificar el table
Código:
TableColumn agregarColumn;
agregarColumn = jTable1.getColumnModel().getColumn(5);
agregarColumn.setCellEditor(new myeditor(jTable1));
agregarColumn.setCellRenderer(new myrenderer(true));
Saludos.
Más información en el Tutorial de Java de SUN:
http://java.sun.com/docs/books/tutor...tml#editrender