tags:

views:

569

answers:

2

Hi I have a class called ColorChooser (in the net.java.dev.colorchooser.ColorChooser package)

This is a custom component used to select colors. What I want is to display a JTable with ColorChoosers in the second column. So I created my own TableCellRenderer and it works:

@SuppressWarnings("serial")
class ColorChooserTableRenderer extends DefaultTableCellRenderer {

    public static List<ColorChooser> colors;

    public ColorChooserTableRenderer(int rows) {
     colors = new ArrayList<ColorChooser>(rows);
     for (int i = 0; i<rows ; i ++) {
      colors.add(new ColorChooser());
     }
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
      boolean isSelected, boolean hasFocus, int row, int column) {
     return colors.get(row);
    }

}

I register this in my table :

JTable t = new JTable(5,3);
t.getColumn(t.getColumnName(1)).setCellRenderer(new ColorChooserTableRenderer(5));

The display is good. It even displays the tool tip of the ColorChoosers when i hover my mouse over one of them. The problem is that the ColorChoosers do not receive MouseEvents.

Normally when you press and hold the mouse on a ColorChooser, you get a pop up window that you can use to select a color. When in the JTable the ColorChooser component does not receive the mouse event.

Any solutions?

Edit: The question can be easily modified to this:

Can you please give me a small example of a table containing JButtons in the second column that actually work? You know, buttons that can be pressed?

+2  A: 

This sounds vaguely familiar as I have been using table cell renderers for other purposes.

My understanding is that TableCellRenderer is only used to render the component; a component does not actually exist at each of the cells.

So you'd probably have to somehow forward mouse events from the JTable itself to the ColorChooser.

edit: p.s., see my question -- also for custom table cell rendering, you only need 1 instance of the component itself for the entire column, if the column is rendered with the same logic. Don't store persistent state in the TableCellRenderer, store it in the TableModel instead, and use that state immediately prior to rendering when you handle the call to getTableCellRendererComponent().

Jason S
+2  A: 

A renderer only paints the component on the screen and does not allow for interaction. What you need is to also implement a TableCellEditor. It is recommend that you inherit the AbstractCellEditor and you'll save some work. Check out the java tutorial for tables.

Example:

public class MyTableCellRenderer implements TableCellRenderer
{
    private JButton button = new JButton("Press Me");
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        return button;
    }
}

public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor
{
    private JButton button;

    public MyTableCellEditor()
    {
        button = new JButton("Press Me");
        button.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                System.out.println("buttonPressed");
            }
        });
    }
    public Object getCellEditorValue() {
        return null;
    }
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        return button;
    }
}
willcodejavaforfood
why is it that Sun makes our lives difficult. Can you please give me a small example of a table containing JButtons in the second column that actually work? You know, buttons that can be pressed?
Savvas Dalkitsis
sure give me a couple of mins :)
willcodejavaforfood
There added a rather pointless example but that is the minimum you need
willcodejavaforfood