how to remove Jbutton from the table
I have a table with a column as JButton.
i set the renderer as follows
TableColumn col = colModel.getColumn(3);
col.setCellRenderer(new MyRenderer("Del"));
col.setCellEditor(new MultiTradeCellEditor(new JCheckBox()));
The renderer and cellEditor classes are
class MyRenderer extends JButton implements TableCellRenderer{
public MyRenderer(Str开发者_如何学编程ing text){
super(text);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
return this;
}
}
}
CellEditor class
class MultiTradeCellEditor extends DefaultCellEditor{
protected JButton button;
public MultiTradeCellEditor(JCheckBox checkBox) {
super(checkBox);
button = new JButton("Del");
button.setOpaque(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectionList.getList().remove(table.getSelectedRow());
table.repaint();
}
});
}
}
When i remove the row from the table. i do model.remove(table.getSelectedRow()). It removes the row except the JButton. I assume that button is part of a Renderer component so it doesnt get removed. How can i do that ?
The Table Button Column example provides renderers and editor for a button as well as an example Action to delete a row from the table.
Odd. Maybe a caching thing?
Try returning an empty label when there is no value?
class MyRenderer extends JComponent implements TableCellRenderer{
private String text;
public MyRenderer(String text){
this.text = text;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (value)
return new JButton(text);
else
return new JLabel();
}
}
}
精彩评论