Regression bug in TreeView.getSelectionModel().getSelectedItem()

Abu Abdullah falcon.sheep at gmail.com
Sat Apr 11 16:58:51 UTC 2020


sample code that represent the issue

*************************
import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TreeViewExample extends Application
{
    public static void main(String[] args)
    {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage)
    {
        TreeItem<String> rootItem = new TreeItem<>("Tutorials");
        TreeItem<String> javaItem = new TreeItem<>("Java Tutorials");
        TreeItem<String> javaItem2 = new TreeItem<>("Java Language");
        TreeItem<String> javaItem3 = new TreeItem<>("Java Collections");
        TreeItem<String> javaItem4 = new TreeItem<>("Java Concurrency");
        javaItem.getChildren().add(javaItem2);
        javaItem.getChildren().add(javaItem3);
        javaItem.getChildren().add(javaItem4);
        rootItem.getChildren().add(javaItem);

        TreeView<String> treeView = new TreeView<>();
        treeView.setRoot(rootItem);

        treeView.setShowRoot(false);

        VBox vbox = new VBox(treeView);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();

        treeView.getSelectionModel().getSelectedItems().addListener(new
ListChangeListener<TreeItem<String>>()
        {
            @Override
            public void onChanged(Change<? extends TreeItem<String>> change)
            {
                int i =
treeView.getSelectionModel().getSelectedItems().size();
                System.out.println(i); // i =1

                TreeItem<String> node =
treeView.getSelectionModel().getSelectedItem();
                if (node == null)
                    System.out.println("node is null"); // In javafx-14
with jdk-14
                else
                    System.out.println("node is not null"); // in javafx
bundled with jdk-8
            }
        });

        treeView.getSelectionModel().clearSelection();
        treeView.getSelectionModel().select(javaItem3);

        final int row = treeView.getRow(javaItem3);
        treeView.scrollTo(row);
    }
}

****************************
this is the important code:
                 if (node == null)
                    System.out.println("node is null"); // In javafx-14
with jdk-14
                else
                    System.out.println("node is not null"); // in javafx
bundled with jdk-8

On Wed, Apr 1, 2020 at 6:38 PM Jeanette Winzenburg <fastegal at swingempire.de>
wrote:

>
> my comment would have been the same as Dirk's :)
>
> As your original description changed between your first and second
> post, I would suggest you write a minimal, reproducible example that
> demonstrates what's _really_ going on in which context. If you think
> it's a bug, please file it (including the mcve)
>
> -- Jeanette
>
> Zitat von Abu Abdullah <falcon.sheep at gmail.com>:
>
> > On Wed, Apr 1, 2020 at 2:05 PM Dirk Lemmermann <dlemmermann at gmail.com>
> > wrote:
> >
> >> You are listening to the changes made to the “list" of selected items
> but
> >> then you are using the “selectedItem” property. If the list gets updated
> >> BEFORE the “selectedItem” value was set then your code will fail.
> Adding to
> >> the list and setting the property is not an atomic operation.
> >>
> >> If this worked before then you were simply lucky. Newer versions of
> JavaFX
> >> might have rearranged the sequence of updates which causes this
> >> “regression”. But I think those were allowed changes. There is no
> contract
> >> that says that the “selectedItem” needs to be set before the list gets
> >> updated.
> >>
> >> Or am I missing something?
> >>
> >
> > Thank you for your response, certainly the list is not updated between
> the
> > 2 calls. it just works all the time in older version but not once in
> newer
> > version.
> >
> > But I forgot very important condition. clicking normally on the tree just
> > works fine (correct behavior) in all versions in the same way. the one
> that
> > is not working is when programmatically select a tree node using:
> > *************************
> > tree.getSelectionModel().clearSelection();
> > tree.getSelectionModel().select(node);
> >
> > final int row = tree.getRow(node);
> > tree.scrollTo(row);
> > *************************
>
>
>
>


More information about the openjfx-discuss mailing list