Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX select item in ListView

Tags:

java

javafx-2

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 .

like image 605
user16380 Avatar asked Jun 18 '12 18:06

user16380


People also ask

How to select item in ListView java?

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.

How to select an item in JavaFX?

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.

How does ListView work 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.

What is ListView java?

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.


1 Answers

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);
    }
});
like image 199
Uluk Biy Avatar answered Nov 08 '22 21:11

Uluk Biy