Adding nodes to a TreeItem in JavaFX


Question

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?

1
4
7/24/2014 2:33:08 AM

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

5
7/24/2014 12:05:29 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon