JavaFX select item in ListView

前端 未结 1 376
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-31 03:30

Hi I am trying to set focus on an item in a listview. After a user opens a file the item is added to the listview, but the issue I am having is that the listview is not sett

相关标签:
1条回答
  • 2020-12-31 04:24

    Assuming that the newly added item has an index of N,
    Selecting it:

    listView.getSelectionModel().select(N);
    

    Focusing on it:

    listView.getFocusModel().focus(N);
    

    Scrolling to it:

    listView.scrollTo(N);
    

    You can use combinations of these and preferably in Platform.runLater().
    Scroll then select:

    Platform.runLater(new Runnable() {
    
        @Override
        public void run() {
            listView.scrollTo(N);
            listView.getSelectionModel().select(N);
        }
    });
    
    0 讨论(0)
提交回复
热议问题