Javafx如何动态地将内容添加到fxml文件中的现有选项卡

Javafx how to add content to an existing tab in fxml file dynamically

我看到很多关于如何将新标签添加到 fxml 文件的现有标签窗格的答案。 我需要将内容添加到选项卡窗格的现有选项卡中。 可能吗?如果是,我该怎么做?

当然可以。只需按照以下步骤操作即可:

  1. 在 FXML
  2. 中将 fx:id 分配给您的选项卡
  3. 使用fx:id绑定控制器中的Tab实例。
  4. 现在,您有了实例,您可以随时向其中添加内容,使用 setContent()

在 FXML 中

...
<Tab fx:id="myTab">
...

在控制器中:

class Mycontroller {

   ...
   @FXML
   private Tab myTab
   ...

   //somewhere in the controller
   tab.setContent(someNode);
   ...
}