1

I have a JTable with custom renderers. When an item in the JTable is clicked, I perform a specific set of actions that affect my model. I have two choices to implement this:

ListSelectionListener : I add a listener on my entire JTable which fires when a row is clicked. Then I perform the actions.

  • Advantages: Allows me to select any part of the row (not required in my case), probably the way Swing intended for events to be fire in a JTable
  • Disadvantages: I have to create a custom class to handle this and reproduce code.

Example:

class Selector implements ListSelectionListener {
    @Override
    public void valueChanged(ListSelectionEvent event) {
        // ... write the action code here
    }
}

JButton & Action : I render a JButton and add an Action to that JButton.

  • Advantages: I can reuse that Action, and if I change that Action, all instances using it will be updated.
  • Disadvantages: I move logic into my rendering code.

Example:

class Renderer implements TableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
                        boolean isSelected, boolean hasFocus, int row, int col) {
        JButton btn = new JButton(value.toString());
        btn.setAction(new SpecificAction());
        return btn;
    }
}
  1. If I use the second solution, will fire rain down from the heavens?

  2. Are they both equally viable?

  3. Is there some way to use Actions inside a ListSelectionListener?

sdasdadas
  • 23,917
  • 20
  • 63
  • 148
  • 1
    Have you considered creating a CellEditor instead to execute the code? – Jonathan Drapeau Feb 19 '13 at 19:42
  • @JonathanDrapeau I didn't mention the CellEditor, but you're correct, Option 2 requires implementation of a CellEditor to actually fire the Action. I've opted to add an anonymous ListSelectionListener and manually create/fire the Action myself. – sdasdadas Feb 19 '13 at 19:57

1 Answers1

2

Without knowledge of your specific use-case, here are my thoughts:

1) I wouldn't use a ListSelectionListener. Some users tend to scroll through the table via the arrow keys. I wouldn't want my listener to fire just because the user is "scrolling" with the arrow keys. However, I have used this for some specific, less-ordinary situations.

2) I would use a renderer to display a JButton in the cell, and then add a MouseListener on the table. Convert the coordinates from the mouseClicked event to a table cell. If the cell has a ButtonRenderer, then fire the appropriate code. This is more likely to filter out unwanted actions since clicking a specific cell is more likely to be an intentional action from the user.

EDIT:

Here's a solution from Rob Camick's site: Table Button Column

And a short discussion about it here: Adding button to JTable

Community
  • 1
  • 1
splungebob
  • 5,357
  • 2
  • 22
  • 45
  • Your (1) is a great point. However, for (2), wouldn't it be better to just set a listener on the button itself instead of manually converting for mouse clicks? – sdasdadas Feb 19 '13 at 19:03
  • Possibly and/or probably. I'd have to think about it some more. – splungebob Feb 19 '13 at 19:12