Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX 2: How to focus a table row programmatically?

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

like image 904
brnzn Avatar asked Dec 06 '13 00:12

brnzn


2 Answers

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);
    }
});
like image 121
trilogy Avatar answered Sep 22 '22 18:09

trilogy


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);
    }
});
like image 45
OttPrime Avatar answered Sep 23 '22 18:09

OttPrime