tags:

views:

39

answers:

2

Hi 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(String 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 ?

A: 

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();
    }

}   


}
willcodejavaforfood
Yes, but cache a reference to the JButton and JLabel and return the same one each time instead of allocating a new one.
Devon_C_Miller
@Devon - I'm lazy when coding without an IDE :)
willcodejavaforfood
+2  A: 

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.

camickr