Saturn
Saturn

Reputation: 18149

Adding nodes to a TreeItem in JavaFX

I have a TreeItem<String> object.

enter image description here

I would like to put a button in it.

Button button = new Button("Hello!");
root.setGraphic(button);  // root is the TreeItem object

enter image description here

Not bad, but I would like to freely position this button. I would like to the right of "Root".

Changing the button's layout X doesn't seem to do anything. How can I do this then?

Upvotes: 4

Views: 5423

Answers (2)

Uluk Biy
Uluk Biy

Reputation: 49185

The quick way will be not to set the valueProperty of tree item and add the desired value as text (or label) to the graphicProperty.

root.setValue("");
Text text = new Text("Root");
Button button = new Button("Hello!");
HBox hbox = new HBox(5);
hbox.getChildren().addAll(text, button);
root.setGraphic(vbox);

The other long way, will be looking up the sub node of tree item and change its properties accordingly.

Edit note: Well it would more appropriate to use HBox instead of VBox. I was in hurry :)

Upvotes: 0

bluevoxel
bluevoxel

Reputation: 5358

You can create custom class extending HBox class and containing constructor declarations with Label and Button initialization. Next, you can use this class to specify TreeView<T> and TreeItem<T> generic types. Sample of such simple HBox custom class can look like this:

import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;

public class CustomItem extends HBox {

    private Label boxText;
    private Button boxButton;

    CustomItem(Label txt) {
        super();

        this.boxText = txt;

        this.getChildren().add(boxText);
        this.setAlignment(Pos.CENTER_LEFT);
    }

    CustomItem(Label txt, Button bt) {
        super(5);

        this.boxText = txt;
        this.boxButton = bt;

        this.getChildren().addAll(boxText, boxButton);
        this.setAlignment(Pos.CENTER_LEFT);
    }
}

And the use of it in practice can look like shown below:

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TreeViewDemo extends Application {

    public Parent createContent() {

        /* layout */
        BorderPane layout = new BorderPane();

        /* layout -> center */

        /* initialize TreeView<CustomItem> object */
        TreeView<CustomItem> tree = new TreeView<CustomItem>();

        /* initialize tree root item */
        TreeItem<CustomItem> root = new TreeItem<CustomItem>(new CustomItem(new Label("Root")));

        /* initialize TreeItem<CustomItem> as container for CustomItem object */
        TreeItem<CustomItem> node = new TreeItem<CustomItem>(new CustomItem(new Label("Node 1"), new Button("Button 1")));

        /* add node to root */
        root.getChildren().add(node);

        /* set tree root */
        tree.setRoot(root);

        /* add items to the layout */
        layout.setCenter(tree);
        return layout;
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setScene(new Scene(createContent()));
        stage.setWidth(200);
        stage.setHeight(200);
        stage.show();
    }

    public static void main(String args[]) {
        launch(args);
    }
}

Effect will look like this:

enter image description here

Upvotes: 5

Related Questions