dhalia
dhalia

Reputation: 421

JavaFX tabbed pane with a table view on each tab?

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.

Why does this not work?!

Thanks!

  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();

Empty:

enter image description here

Upvotes: 4

Views: 6309

Answers (1)

James_D
James_D

Reputation: 209225

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

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()));

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.

Upvotes: 4

Related Questions