在对象内的对象的单独窗口中绘制子表

时间:2015-06-25 14:17:36

标签: java javafx

我们说List<String> lstcat包含两个表格中我需要的整个类别列表。

我也有ObservableList<ObjectA> lstObject,其定义如下:

public class ObjectA {
    String MPID;
    String Date;
    ObjectB insideObject;
    }

将对象B定义为:

public class ObjectB {
    String Symbol;
    int Age;
    }

所以lstObject = [ObjectA1, ObjectA2, ...]

我的目标是让一个表格显示所有对象A的成员,当我点击该表格中的一行时,会出现另一个表格,其中包含相应的对象B的成员。< / p>

我有第一张表的代码:

private TableView init(Stage primaryStage) throws Exception {
    Group root = new Group();
    primaryStage.setScene(new Scene(root));
    TableView<ObjectA> tableView = new Tableview<>;
    List<String> lstCat = ...
    ObservableList<ObjectA> lstObject = ...

    for (int i =0; lstCat.get(i).equals("Symbol") == false;i++) {
        TableColumn<ObjectA,String> col = new TableColumn<>(lstCat.get(i));
        col.setCellValueFactory(new PropertyValueFactory<>(lstCat.get(i)));
        tableView.getColumns().add(col);
        }
    tableView.setItems(lstObject);
    return tableView;
}


public void start(stage primaryStage) throws Exception {
    TableView firstTable = init(primaryStage);
    // Here's the code I need help with. Right now I just have a print statement, 
    // I need this to generate a new table with the corresponding objectB.
    firstTable.setOnMouseClicked(new EventHandler<MouseEvent>()
    {
        @Override
        public void handle(MouseEvent t) {
            System.out.println("print when click")
            }
            });
    Scene scene = new Scene(test);
    primarystage.setScene(scene);
    primaryStage.show();
    }

第一张桌子很好看,没有任何问题。

到目前为止我的工作:

我知道要创建新表的列: 这将进入handle方法。

TableView<ObjectB> innerTable = ....
for (int i = //element in lstCat where objectB members start; i<lstCat.size();i++) {
    TableColumn<ObjectA,String> col = new TableColumn<>(lstCat.get(i));
        col.setCellValueFactory(new PropertyValueFactory<>(lstCat.get(i)));
        innerTable.getColumns().add(col);
        }

但是,这不起作用,因为lstObject的类型为ObjectA,但innerTable的类型为ObjectB。所以我收到了错误。

我在这里缺少什么?

(我动态这样做的原因是因为我实际使用的表有比这更多的列,所以静态不是一个选项。

谢谢!

编辑:

我相信我可能已经弄清楚了。

我可以与lstObject进行互动并制作lstObjectB并将每个ObjectB的每个ObjectA添加到ObservableList,然后只需innerTable那样。

除非有更好的方法吗?

另外,我如何使用mouseEvent显示一个带有新innertable的新窗口?

它也可能只是一个子行或其他东西。不一定是新窗口,只是弹出显示新信息的其他内容。

1 个答案:

答案 0 :(得分:0)

After a little bit of thought I did figure it out on my own.

It may not be the cleanest/most efficient way, but it is quick and dirty and allows you to keep moving forward.

I made a return function for Class A's ObjectB.

Public class ObjectA {
    ...
    public ObjectB getObjectB() {
        return ObejctB;
    }

And then, just as I said, I iterated over lstObject to pull out ObjectB.

So I ended up with something that looked like this:

private static ObservableList<ObjectA> getLstObject() {
    return new lstObject....
}

Then I made another function:

private static ObservableList<ObjectB> getListObjectB() {
        ObservableList<ObjectA> lstObjectA =  getLstObject;
        ObservableList<ObjectB> lstObjectB = new FXCollection.observableArraylist();
        for (int i =0;i<lstObjectA.size();i++) {
            lstObjectB.add(lstObjectA.getObjectB());
        }
        return lstObjectB
    }

Now I had two observable lists, so making the Tableview for each was what I had done before, and it was all good.