1

Using a JTable, my Table Model setValueAt() method moves the selection to the next row in certain cases, using setRowSelectionInterval() and setColumnSelectionInterval(). When it's called from the (default) cell editor (by user typing in the cell and hitting tab), the code works: the desired next cell is selected (the first one on the next row).

However, if the user uses Return rather than Tab to commit the edit, the selection doesn't happen; instead the cell below is selected. That's fine with me.

I also have a JButton to clear a row. The button's action function calls the model's setValueAt() function for the desired cells. Unfortunately, the setRowSelectionInterval() and setColumnSelectionInterval() methods have no apparent effect; instead, no cells are selected.

I've tried table.requestFocusInWindow() and table.getParent().requestFocusInWindow(), as well as table.changeSelection(row, 0, false, false), all to no apparent effect.

Is there anything basic I'm missing here, before I go to the trouble of building the SSCCE?

In case it matters, here's the container hierarchy:

parent JPanel
  button rows JPanel
    button row 1 JPanel
    button row 2 JPanel
  table JScrollpane
    JTable

The button in question is in button row 1.

Thanks!

Jeff Learman
  • 2,914
  • 1
  • 22
  • 31
  • 1
    If this were my problem and I needed help, I'd create and post a [minimal example program, an mcve](http://stackoverflow.com/help/mcve) and hope that by providing the smallest program that demonstrates the problem, someone would be able to figure out a solution. – Hovercraft Full Of Eels Feb 09 '15 at 22:27
  • 2
    `before I go to the trouble of building the SSCCE?` Building a SSCCE is not trouble. It is a problem solving tool that you should always create to make sure you understand the problem. By the way the TableModel should NOT be changing selection of the table, so you should NOT be overriding the setValueAt() method. The TableModel just manages the data, the View handles the selection. – camickr Feb 09 '15 at 22:38
  • It seems like you could evoke the `"selectNextRow"` action, as suggested [here](http://stackoverflow.com/a/20413603/230513) for scrolling actions. – trashgod Feb 09 '15 at 22:39
  • @camickr: I'm confused as I thought that you would want to override `setValueAt(...)` if you the table cell was editable. – Hovercraft Full Of Eels Feb 09 '15 at 22:42
  • 1
    @HovercraftFullOfEels, the TableModel should not invoke `setRowSelectionInterval(...)` in the TableModel since that is modifying the View of the table. At least that is how I understand the separation of Model and View. – camickr Feb 09 '15 at 22:44
  • @camickr: Good point that I'm violating separation of view and model by changing the selection here. However, I had the same problem (no effect) when calling the functions from the button's listener. – Jeff Learman Feb 10 '15 at 16:15
  • @HovercraftFullOfEels: I agree, and thanks for helping me not thinking I'm totally nuts. The example at http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data implements setValueAt. But it doesn't (and shouldn't) fiddle with view aspects. – Jeff Learman Feb 10 '15 at 16:18
  • @trashgod: I can't find "selectNextRow" at your link or anywhere. – Jeff Learman Feb 10 '15 at 16:21

1 Answers1

4

Maybe you can use the Table Cell Listener to listen for edits to the table. It listens for actual changes done by the JTable editor.

Then in the supplied Action you can select the appropriate row. You may need to wrap the Action code in a SwingUtilities.invokeLater(...) to make sure the code executes after the table is completely finished editing.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks! Actually, just using invokeLater() in setValueAt() causes the symptom to go away, but as noted above, it's not the correct solution. The problem is, I have the horse inside the cart! – Jeff Learman Feb 10 '15 at 16:30