JavaFX选项卡式窗格,每个选项卡上都有一个表视图? [英] JavaFX tabbed pane with a table view on each tab?

查看:514
本文介绍了JavaFX选项卡式窗格,每个选项卡上都有一个表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标签式窗格,每个标签上都有一个表格。我向表中添加了不同的项目,我只想让每个选项卡显示该表的各个项目。但没有任何表现。
当我调试时,我可以清楚地看到包含选项卡的选项卡窗格,其中包含包含正确项目的表视图。

I have a tabbed pane, with a table on each tab. I add different items to the tables and I just want each tab to show me the respective items of that table. But nothing shows up. When I debug I can clearly see the tab pane, that contains the tabs, that contains the table views, that contain the right items.

为什么这不起作用?!

谢谢!

  primaryStage.setTitle("TEST");
  Group root = new Group();
  Scene scene = new Scene(root, 400, 300, Color.WHITE);

  TableView tv1 = new TableView();
  TableView tv2 = new TableView();

  TableColumn<String, String> clmn = new TableColumn<>("Test");
  clmn.setPrefWidth(230);

  tv1.getColumns().addAll(clmn);
  tv2.getColumns().addAll(clmn);

  List<String> firstItems = new ArrayList<>();
  firstItems.add("ONE");
  firstItems.add("TWO");
  firstItems.add("THREE");
  tv1.setItems(FXCollections.observableArrayList(firstItems));

  List<String> secondItems = new ArrayList<>();
  secondItems.add("1");
  secondItems.add("2");
  secondItems.add("3");
  tv2.setItems(FXCollections.observableArrayList(secondItems));

  TabPane tabPane = new TabPane();
  BorderPane mainPane = new BorderPane();

  //Create Tabs
  Tab tabA = new Tab();
  tabA.setText("Tab A");
  tabA.setContent(tv1);
  tabPane.getTabs().add(tabA);

  Tab tabB = new Tab();
  tabB.setText("Tab B");
  tabB.setContent(tv2);
  tabPane.getTabs().add(tabB);

  mainPane.setCenter(tabPane);
  mainPane.prefHeightProperty().bind(scene.heightProperty());
  mainPane.prefWidthProperty().bind(scene.widthProperty());  
  root.getChildren().add(mainPane);
  primaryStage.setScene(scene);
  primaryStage.show();

空:

推荐答案

您的代码有两个问题(我立即看到):

There are two problems with your code (that I see immediately):

您需要在 TableView的每一列上设置一个单元格值工厂。如果没有这个,列就没有任何要显示的数据。所以你需要像

You need to set a cell value factory on each column of a TableView. Without this, the column doesn't have any data to display. So you need something like

clmn.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue()));

其次,您使用相同的 TableColumn 对于两个表。我不认为这可行;单元格最终将被添加到两个不同位置的场景图形中(这是不允许的)。为每个表创建不同的列。

Secondly, you are using the same TableColumn for both tables. I don't think this can work; the cells would end up being added to the scene graph in two different places (which is not allowed). Create different columns for each table.

这篇关于JavaFX选项卡式窗格,每个选项卡上都有一个表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆