简体   繁体   中英

Programmatically edit TreeView/TreeItem

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 this post up-to-date, if there is new information.

Original post: I have a simple UI with a button and a TreeView. If the button gets pressed there should be a new item added to the TreeView. This item should be editable as soon as it appears in the tree.

import javafx.fxml.FXML;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.util.converter.DefaultStringConverter;

public class ClientController {

    @FXML
    private TreeView<String> tree;

    public void initialize() {
        tree.setEditable(true);
        tree.setCellFactory(p -> new TextFieldTreeCell<>(new DefaultStringConverter()));

        TreeItem<String> root = new TreeItem<>();
        root.setValue("Items");
        root.setExpanded(true);

        tree.setRoot(root);
    }

    @FXML
    public void createItem() {
        TreeItem<String> newItem = new TreeItem<>();
        newItem.setValue("Item " + tree.getExpandedItemCount());

        tree.getRoot().getChildren().add(newItem);
        tree.requestFocus();
        tree.getSelectionModel().select(newItem);
        tree.edit(newItem);
    }
}

The CellFactory i am using is part of the JavaFX api .

If I have a look at the api-documentation ( TreeView#edit ) there is not much to do on my site.

Many examples I found via google like this create a context-menu for each TreeItem. Useful, but not exactly what i want, right now.

If I select/double-click the item in the UI I am able to edit any previously created and existing TreeItem. Do I miss something?

Edit #1:

If the createItem method gets changed to the following code:

@FXML
public void createItem() throws InterruptedException {
    TreeItem<String> newItem = new TreeItem<>();
    newItem.setValue("Item " + this.tree.getExpandedItemCount());

    this.tree.getRoot().getChildren().add(newItem);
    this.tree.requestFocus();
    this.tree.getSelectionModel().select(newItem);
    Thread.sleep(100);
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            SimpleController.this.tree.edit(newItem);
        }
    });
}

every new item is correctly marked for edit (the created textfield and its content are selected). Using Platform.runLater without Thread.sleep doesnt work and Thread.sleep without runLater neither.

This simply does not feel right. What is my problem?

I provide a very small example (eclipse) project: https://www.dropbox.com/s/duos0ynw4rqp3yn/Test.zip?dl=0 containing The main-method, the FXML-File and the "problematic" controller.

TreeCells (that is their content and binding to the treeItem) are updated lazily in a layout pass, that is very "late" when the next pulse is fired. A Platform.runLater(..) or any hard-coded delay may happen before or after that pulse, that's (probably) why both seem to work or not spuriously.

So another whacky hack is to manually force a re-layout on the tree after having added a new item and before programatically starting an edit:

root.getChildren().add(newItem);
tree.layout();
tree.edit(newItem);

Needless to say, that this shouldn't be necessary - the current behaviour is a severe bug and must be fixed at once (... meaning ... jdk 9)

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<String> newItem = new TreeItem<>();
    newItem.setValue("Item " + tree.getExpandedItemCount());

    tree.getRoot().getChildren().add(newItem);

    // Trigger whatever calculations there internally
    TreeItem<String> 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<ActionEvent>()
{
    @Override
    public void handle( ActionEvent event )
    {
        tree.edit( newItem );
    }
} );
p.play();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM