If a TreeItem gets added to a TreeView it is not immediately programmatically editable. It looks like the treeview is updated to late.
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.HBox?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SimpleController">
<center>
<TreeView fx:id="tree" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
<top>
<Button mnemonicParsing="false" onAction="#createItem" text="create item" BorderPane.alignment="TOP_LEFT" />
</top>
</BorderPane>
Main.java
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(final Stage primaryStage) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getResource("Test.fxml"));
fxmlLoader.load();
Scene scene = new Scene(fxmlLoader.getRoot(), 300, 200);
primaryStage.setScene(scene);
primaryStage.setTitle("Test");
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(final String[] args) {
Application.launch(args);
}
}
SimpleController.java:
package application;
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 SimpleController {
@FXML
private TreeView<String> tree;
public void initialize() {
this.tree.setEditable(true);
this.tree.setCellFactory(p -> new TextFieldTreeCell<>(new DefaultStringConverter()));
TreeItem<String> root = new TreeItem<>();
root.setValue("Items");
root.setExpanded(true);
this.tree.setRoot(root);
}
@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);
this.tree.edit(newItem); //this should be enough, no?
}
}
Steps to reproduce:
1. start the application
2. press the button "create item"
3. every button press creates a new item, *but* it is not editable.
If TreeView#layout() is called immediately before TreeView#edit it is working as expected.
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.HBox?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SimpleController">
<center>
<TreeView fx:id="tree" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
<top>
<Button mnemonicParsing="false" onAction="#createItem" text="create item" BorderPane.alignment="TOP_LEFT" />
</top>
</BorderPane>
Main.java
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(final Stage primaryStage) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getResource("Test.fxml"));
fxmlLoader.load();
Scene scene = new Scene(fxmlLoader.getRoot(), 300, 200);
primaryStage.setScene(scene);
primaryStage.setTitle("Test");
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(final String[] args) {
Application.launch(args);
}
}
SimpleController.java:
package application;
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 SimpleController {
@FXML
private TreeView<String> tree;
public void initialize() {
this.tree.setEditable(true);
this.tree.setCellFactory(p -> new TextFieldTreeCell<>(new DefaultStringConverter()));
TreeItem<String> root = new TreeItem<>();
root.setValue("Items");
root.setExpanded(true);
this.tree.setRoot(root);
}
@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);
this.tree.edit(newItem); //this should be enough, no?
}
}
Steps to reproduce:
1. start the application
2. press the button "create item"
3. every button press creates a new item, *but* it is not editable.
If TreeView#layout() is called immediately before TreeView#edit it is working as expected.