问题描述
我正在尝试将重点放在列表视图中的某个项目上.用户打开文件后,该项目将添加到列表视图中,但是我遇到的问题是列表视图未将焦点设置在已添加的新项目上.我必须单击列表视图中的项目以将焦点设置为该项目.有没有一种方法可以让listview立即在JavaFX 2.1中突出显示新添加的项目.
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 setting focus on the new item that was added. I have to click the item in the listview to set focus to it. Is there a way to have the listview to highlight the newly added item right away in JavaFX 2.1 .
推荐答案
假定新添加的项的索引为N
,
选择它:
Assuming that the newly added item has an index of N
,
Selecting it:
listView.getSelectionModel().select(N);
关注它:
listView.getFocusModel().focus(N);
滚动到它:
listView.scrollTo(N);
您可以将这些组合使用,最好在Platform.runLater()
中使用.
滚动然后选择:
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);
}
});
这篇关于JavaFX在ListView中选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!