Programmatically edit TreeView/TreeItem

后端 未结 2 857
后悔当初
后悔当初 2020-12-21 09:54

Edit #2: Since it looks like a bug i already posted a bug report in the javaFx-jira. You have to have an account to have access to the issue. I will keep th

2条回答
  •  一向
    一向 (楼主)
    2020-12-21 10:29

    It seems, the treeView is needing more time to do its internal calculations before the newly added item got reflected to it. To determine exactly which calculations are causing the problem, one need to dig the source code and see what is going on under the hood.

    The workaround is to force treeView do its calculation immediately,

    @FXML
    public void createItem() throws InterruptedException {
        TreeItem newItem = new TreeItem<>();
        newItem.setValue("Item " + tree.getExpandedItemCount());
    
        tree.getRoot().getChildren().add(newItem);
    
        // Trigger whatever calculations there internally
        TreeItem r = tree.getRoot();
        tree.setRoot( null );
        tree.setRoot( r );
    
        tree.requestFocus();
        tree.getSelectionModel().select(newItem);
        tree.edit(newItem);
    }
    

    This workaround was found totally intuitively based on other bugs in JavaFX ;-)


    Alternatively, since Platform.runlaters's postponed time is obviously not enough, you may use PauseTransition to increase the time needed:

    PauseTransition p = new PauseTransition( Duration.millis( 100 ) );
    p.setOnFinished( new EventHandler()
    {
        @Override
        public void handle( ActionEvent event )
        {
            tree.edit( newItem );
        }
    } );
    p.play();
    

提交回复
热议问题