英文:
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<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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论