Search code examples
javajavafxtreeview

How to insert multiple different objects (classes) in a TreeView using javaFX


I am trying to create a TableView in which there are different classes/object types per level. My TreeView would look something like this, where Object A.1 would be the first instance of Class A, Object A.2 the second, etc..

>RootItem
    >Object A.1
        >Object B.1
        >Object B.2
        >Object C.1
    >Object A.2
        >Object B.3
            >Object D.1

I only need to display the name of the objects (ie the toString method), but I also need the objects to be more than just Strings, so I can access the other attributes at a later point (ie if the user double clicks on an object).

How would this have to be implemented? Is there maybe another approach I didn't think of?


Solution

  • import java.io.IOException;
    
    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.TreeItem;
    import javafx.scene.control.TreeView;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class SOTest extends Application{
    
        public static void main(String[] args) throws IOException  {
            launch(args);
        }
    
        @Override
        public void start(Stage stage) throws Exception {
            //Creating tree items
            TreeItem<Parent> root1 = new TreeItem<Parent>(new A("A.1"));
            TreeItem<Parent> item1 = new TreeItem<Parent>(new B("B.1"));
            TreeItem<Parent> item2 = new TreeItem<Parent>(new B("B.2"));
            TreeItem<Parent> item3 = new TreeItem<Parent>(new C("C.1"));
            root1.setExpanded(true);
            root1.getChildren().addAll(item1, item2, item3);
            
            
            TreeItem<Parent> root2 = new TreeItem<Parent>(new A("A.2"));
            item1 = new TreeItem<Parent>(new B("B.3"));
            TreeItem<Parent> subItem = new TreeItem<Parent>(new D("D.1"));
            item1.getChildren().add(subItem);
            root2.getChildren().addAll(item1);
    
            
            TreeItem base = new TreeItem("Root Node");
            base.setExpanded(true);
            base.getChildren().addAll(root1, root2);
            //Creating a TreeView item
            TreeView view = new TreeView(base);
            view.setPrefHeight(300);
            VBox pane = new VBox(10);
            pane.setPadding(new Insets(5, 5, 5, 50));
            pane.getChildren().addAll(view);
            //Setting the stage
            Group node = new Group(pane);
            Scene scene = new Scene(node, 595, 320);
            stage.setTitle("Diff Object Tree Structure");
            stage.setScene(scene);
            stage.show();
        }
    }
    
    abstract class Parent{
        protected String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
        
        public String toString() {
            return name;
        }
    }
    
    class A extends Parent{
        public A(String name) {
            this.name = name;
        }
        public B b;
        public C c;
    }
    
    class B extends Parent{
        public B(String name) {
            this.name = name;
        }
        public D d;
    }
    
    class C extends Parent{
        public C(String name) {
            this.name = name;
        }
    }
    
    class D extends Parent{
        public D(String name) {
            this.name = name;
        }
    }
    

    enter image description here