ListView not showing selected item when selected programatically


I have two list views that I am using in a JavaFX project that represent categories of items. The first one is set with a single observable list during initialization and never changes. The second ListView, however, changes based on which item is selected from the first ListView. Along with a bunch of other items, the selected items are saved to an object for later editing.

I reuse this same fxml file and controller when a user wants to edit one of their saved objects. The problem that I am having is that while I can programatically show the first list view, its observable list, and the selected item, and also show the second list view and the appropriate observable list based on the saved item's catagory1 data field, I can't seem to get it to show that the item is selected, nor can I get it to scroll the second ListView when it should. Any help would be very appreciated!

Here's the parts of my code that deal with these actions...

First, this part is in the initialize() section:



ObservableList items = category2ListView.getItems();
int range = items.size();

for(int count = 0; count < range; count++){
    String item = (String) items.get(count);

And here's "showCategory2()" for reference:

public void showCategory2(){
    String selectionString = category1ListView.getSelectionModel().getSelectedItem();

    if(selectionString.matches("Art")) {
        category2ListView = new ListView<String>(artList);
    } else if (selectionString.matches("Books")){
        category2ListView = new ListView<String>(booksList);
    } else if (selectionString.matches("Decorative Accessores")){
        category2ListView = new ListView<String>(decorativeAccessoriesList);
    } else if (selectionString.matches("Floor Coverings")){
        category2ListView = new ListView<String>(floorCoveringsList);
    } else if (selectionString.matches("Furniture")){
        category2ListView = new ListView<String>(furnitureList);
    } else if (selectionString.matches("Kitchen & Tabletop")){
        category2ListView = new ListView<String>(kitchenAndTabletopList);
    } else if (selectionString.matches("Lighting")){
        category2ListView = new ListView<String>(lightingList);

etc... through the rest of my lists. Some of the lists have only a few items in them, others have anywhere from 7 - 15 items, which is more than what is able to be viewed in the second listView. I do not want to alter the size of the list view to accommodate for the largest list. I would much rather it scrolled as appropriate.

NOTE: I have thrown in a few test bits of code like:

System.out.println("Selected item: " 
+ category2ListView.getSelectionModel().getSelectedItem());

which shows the appropriate item that was previously selected... but the second list view itself is not showing which item is selected, nor is it scrolling to the correct item when it should.

Any ideas?

EDIT: After some testing with my code, I have discovered that the issue seems to occur only when I also use the ListView.scrollTo() method. If I remove the scrollTo method from my code, the list view correctly shows and highlights the selected item.

This is rather inconvenient, however, as it means that any list I have longer than the size of my listView will not scroll down to the selected item.

7/2/2013 8:58:44 PM

Try next code

        if (this.textToSearch.getText().length() > 0) {
            for (int i = 0; i < this.listDirectories.getItems().size(); i++) {
                SMPBDirectory oneDir = (SMPBDirectory) this.listDirectories.getItems().get(i);
                if ( {
//Now magic

For me it's working,but i use JavaFX 8.

For now it's fixed.

11/20/2013 6:08:27 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow