1

Can you list possible reasons why calling tableView.getSelectionModel().select(0); on a javafx.scene.control.TableView named tableView won't work?

For this case, let's say I'm using cellSelectionEnabled(false) and SelectionMode.SINGLE, and tableView.getItems() returns a non-empty FXCollections.observableArrayList.

To be more precise, I'm looking for possible answers why there can be instances wherein calling tableView.getSelectionModel().select(0); from a method like selectTableViewRow0() won't work:

private void selectTableViewRow0(){
    tableView.getSelectionModel().select(0);
}

Any suggestion, guys?

Edit:
As mentioned in the comments below, calling tableView.getSelectionModel().isSelected(0); returns true. I'm guessing I can use reflection to call the code in TableView that highlights the selected row. The problem is I can't seem to find out which part of the TableView code I should call. I figured calling select(0) will lead to calling the following two lines internally. But after that there seems to be no code telling the TableView to highlight the selected row.

updateSelectedIndex(row);
focus(row, column);
damat-perdigannat
  • 5,780
  • 1
  • 17
  • 33
  • 2
    Seems similar to [RT-30356 Selection bar in ListView not always displayed for a selected item](https://javafx-jira.kenai.com/browse/RT-30356), but perhaps it's different. Update your question to include full environment details and an [sscce](http://sscce.org/). – jewelsea Jul 31 '13 at 02:38
  • Hi, I have already included the environment details. However, I can't seem to replicate the error using an ssce. It might be much to ask but can you/someone provide a workaround for selecting an item using reflections probably? Thanks. – damat-perdigannat Jul 31 '13 at 04:33
  • I don't have any such workaround. – jewelsea Jul 31 '13 at 05:01
  • Alright thanks man. I'll try to update this post once my problem is solved. – damat-perdigannat Jul 31 '13 at 05:33

2 Answers2

3

Have you tried

Platform.runLater(new Runnable() {
    public void run() {
        tableView.getSelectionModel().select(0);
    }
});
jhsheets
  • 500
  • 3
  • 10
  • Will try. I'll comment after trying. – damat-perdigannat Jul 31 '13 at 04:25
  • 1
    What happens if you call: tableView.getSelectionModel().isSelected(0) right after? Is the result true? Maybe it's not triggering because it thinks the row is already selected and not firing a change event. Does tableView.getSelectionModel().clearAndSelect(0) work? – jhsheets Jul 31 '13 at 05:00
  • Will try. I'll comment after trying. – damat-perdigannat Jul 31 '13 at 05:14
  • It turns out that tableView.getSelectionModel().isSelected(0) returns true after tableView.getSelectionModel().select(0); and tableView.getSelectionModel().clearAndSelect(0);. However, the selected row doesn't highlight. I'm guessing this has something to do with the TableView's skin? – damat-perdigannat Jul 31 '13 at 05:27
  • Does the row highlight if you click on it? Unless you changed something, highlighting should work. Check to see if you're setting -fx-background-color anywhere in a CSS file or in a setStyle() method. Check if you have any TableRowFactory classes, or if you're doing something weird in your CellValueFactory's. You might want to try tableView.getSelectionModel().setSelectedIndex(0) as well; I've never used the select() method myself. I'm not sure if select() does something weird like not trigger a table refresh to show the highlighted row. – jhsheets Jul 31 '13 at 05:42
  • Also, it can be difficult to tell when a row is highlighted on a TableView if the table view isn't focused. Double-check that you're not just mistaken about the row being highlighted. – jhsheets Jul 31 '13 at 05:47
  • Yes, the row highlights when I click on it. I didn't do any styling though. Will try setSelectedIndex and tableView.requestFocus() as well and then I'll comment for updates. Thank you so much. – damat-perdigannat Jul 31 '13 at 05:56
  • Hi, as checked, setSelectedIndex is not a method for getSelectionModel(), while requestFocus() showed that the row wasn't highlighted. – damat-perdigannat Jul 31 '13 at 07:08
  • 1
    Looks like setSelectedIndex() is a protected method. Sorry, all out of ideas. Maybe file a bug report, or try using a different Java7 build. – jhsheets Jul 31 '13 at 12:25
  • Thanks a lot man. Already filed a bug report for this. Although I don't know if they'll accept it without a short simple and compilable example. Also edited the question fyi. – damat-perdigannat Jul 31 '13 at 12:35
-1

Thanks to jewelsea and jhsheets for contributing. One reason I found for this problem is that when a similar scene comes into play, and then the old scene is not garbage collected, the old table view item (from the old scene) will be selected. Luckily, I found a related bug, which I can discuss if someone is interested.

damat-perdigannat
  • 5,780
  • 1
  • 17
  • 33