JavaFX 2: How to focus a table row programmatically?
Asked Answered
G

4

13

I am trying to select/focus a row of a TableView programmatically.

I can select a row, but it is not getting rendered as focused (not highlighted). I have tried many combinations of the code below, but nothing seems to work.

table.getSelectionModel().select(0);
table.focusModelProperty().get().focus(new TablePosition(table, 0, column));
table.requestFocus();

Is it possible to highlight a row programmatically?

I am using JavaFX 2.2.21

Glabella answered 6/12, 2013 at 0:5 Comment(0)
A
19

Try putting your request for table focus first and then wrapping the whole thing in a runLater.

Platform.runLater(new Runnable()
{
    @Override
    public void run()
    {
        table.requestFocus();
        table.getSelectionModel().select(0);
        table.getFocusModel().focus(0);
    }
});
Angelus answered 6/12, 2013 at 21:33 Comment(2)
Works :-) Why the order matter?Glabella
This is my assumption, as I haven't done a lot of research on the FX focus model as a whole. It appears that the table row focus is more of a pseudo-focus that is managed by the table. When you request focus for the table after the row, the table acts as though it is defaulting focus which may or may not be on the row you want. Interestingly, I have also had row focus fail if I didn't request focus to the table first. I'm sorry I can't give you a definitive answer.Angelus
B
2

table.getFocusModel().focus(0); is not needed, but I would also add scrollTo as well.

Java 8:

Platform.runLater(() ->
  {
      table.requestFocus();
      table.getSelectionModel().select(0);
      table.scrollTo(0);
  });

Java 7:

Platform.runLater(new Runnable()
{
    @Override
    public void run()
    {
        table.requestFocus();
        table.getSelectionModel().select(0);
        table.scrollTo(0);
    }
});
Beekeeping answered 2/10, 2018 at 13:59 Comment(0)
P
1

I have two components: a ListView and a TableView. When an item in the ListView is clicked, I want the focus and selection to move to the TableView and render the selected component in the TableView. To accomplish this, I did it with:

void listViewClickHandler(MouseEvent e){
    A a = listView.getSelectionModel().getSelectedItem();
    if(a != null){
        // some stuff

        // move focus & selection to TableView
        table.getSelectionModel().clearSelection(); // We don't want repeated selections
        table.requestFocus();                       // Get the focus
        table.getSelectionModel().selectFirst();    // select first item in TableView model
        table.getFocusModel().focus(0);             // set the focus on the first element
        tableClickHandler(null);                    // render the selected item in the TableView
}

void tableClickHandler(MouseEvent e){
    B b = table.getSelectionModel().getSelectedItem();
    render(b);
}
Pleading answered 18/9, 2014 at 14:14 Comment(0)
P
0

table.getSelectionModel().select(0); works for me. Maybe the problem is in your css?

Postorbital answered 6/12, 2013 at 0:24 Comment(1)
I don't have any custom css. Maybe something else happen after I select a row.Glabella

© 2022 - 2024 — McMap. All rights reserved.