如何在使用JavaFX的TreeView中插入多个不同的对象(类)

huangapple go评论66阅读模式
英文:

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

问题

我正在尝试创建一个 TableView,其中每个级别都有不同的类/对象类型。
我的 TreeView 大致会像这样,其中 Object A.1 将是 Class A 的第一个实例,Object A.2 是第二个实例,依此类推:

    > 根项目
        > 对象 A.1
            > 对象 B.1
            > 对象 B.2
            > 对象 C.1
        > 对象 A.2
            > 对象 B.3
                > 对象 D.1

我只需要显示对象的名称(即 toString 方法),但我还需要这些对象不仅仅是字符串,这样我以后可以访问其他属性(例如,如果用户双击对象)。

这应该如何实现?
也许还有另一种方法我没有考虑到?
英文:

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?

答案1

得分: 1

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 {
        // 创建树节点
        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);

        // 创建TreeView节点
        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);

        // 设置舞台
        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;
    }
}
英文:
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&lt;Parent&gt; root1 = new TreeItem&lt;Parent&gt;(new A(&quot;A.1&quot;));
TreeItem&lt;Parent&gt; item1 = new TreeItem&lt;Parent&gt;(new B(&quot;B.1&quot;));
TreeItem&lt;Parent&gt; item2 = new TreeItem&lt;Parent&gt;(new B(&quot;B.2&quot;));
TreeItem&lt;Parent&gt; item3 = new TreeItem&lt;Parent&gt;(new C(&quot;C.1&quot;));
root1.setExpanded(true);
root1.getChildren().addAll(item1, item2, item3);
TreeItem&lt;Parent&gt; root2 = new TreeItem&lt;Parent&gt;(new A(&quot;A.2&quot;));
item1 = new TreeItem&lt;Parent&gt;(new B(&quot;B.3&quot;));
TreeItem&lt;Parent&gt; subItem = new TreeItem&lt;Parent&gt;(new D(&quot;D.1&quot;));
item1.getChildren().add(subItem);
root2.getChildren().addAll(item1);
TreeItem base = new TreeItem(&quot;Root Node&quot;);
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(&quot;Diff Object Tree Structure&quot;);
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;
}
}

如何在使用JavaFX的TreeView中插入多个不同的对象(类)

huangapple
  • 本文由 发表于 2020年10月12日 02:04:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64307380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定