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 .
To select a single list view item, you can use various actions provided by Android ListView object: TouchItem , LongTouchItem , and similar actions – simulate touch or long touch on a specific list view item.
The line, radio1. setSelected(true);, allows us to select the first radio button by default. Running the above code produces the image at the following link: Radio Button Selected By Default. And this is all that is required to select an item by default in JavaFX.
A ListView displays a horizontal or vertical list of items from which the user may select, or with which the user may interact. A ListView is able to have its generic type set to represent the type of data in the backing model.
A ListView is a type of AdapterView that displays a vertical list of scroll-able views and each view is placed one below the other. Using adapter, items are inserted into the list from an array or database.
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);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With