如何使用JavaFX实际隐藏TabPane中的选项卡

5

之前我一直在使用Java Swing,现在尝试使用JavaFX。这是我上次的Java Swing代码:

//These line of code is to call method that declared in ContentPage.java
contentPage.adminFeatureEnabled(adminEnabled);
contentPage.managerFeatureEnabled(managerEnabled);

并且在我的ContentPage.java文件中

//By default, all feature (or tab) are enabled.
//This method is to remove register account if the user login into the system is manager and staff
public void adminFeatureEnabled(boolean a) {
    if (!a) {
        tabPane.removeTabAt(tabPane.indexOfComponent(registerAccount));
    }
}
//This method is to remove register account and purchase order if the user who log into the system is staff
public void managerFeatureEnabled(boolean a) {
    if(!a) {
        tabPane.removeTabAt(tabPane.indexOfComponent(purchaseOrder));
    }
}

在我的代码中:
if (role.equals("admin")){
     contentPage.contentFrame.setTitle("Menu - Admin!");
     contentPage.disUser.setEditable(true);
     contentPage.chgRoles.setEnabled(true);
} else if(role.equals("manager")){
     contentPage.contentFrame.setTitle("Menu - Manager!");
     contentPage.chgRoles.setSelectedItem("manager");
     adminEnabled = false;
}else if (role.equals("staff")){
     contentPage.contentFrame.setTitle("Menu - Staff!");
     contentPage.chgRoles.setSelectedItem("staff");
     adminEnabled = false;
     managerEnabled = false;
}

以上代码将会执行以下操作:

  1. 当用户使用管理员帐户登录时,所有功能(选项卡)都将启用。
  2. 当用户以经理身份登录时,某些功能(选项卡)将被隐藏。

我目前的问题是:
我想在JavaFX中实现与上述相同的功能,但我不知道如何实现,因为没有任何方法符合我的要求。

有人可以帮我吗?


相关(虽然不是特定于选项卡):有没有办法在JAVAFX上实现类似“rendered”的属性? - jewelsea
2个回答

6

只需修改tabs列表:

以下示例在选择/取消选择CheckBox时添加/删除Tab

@Override
public void start(Stage primaryStage) {
    Tab tab1 = new Tab("Tab 1", new Label("1"));
    Tab tab2 = new Tab("Tab 2", new Label("2"));

    TabPane tabPane = new TabPane();
    tabPane.setPrefSize(400, 400);

    CheckBox cb1 = new CheckBox("1");
    CheckBox cb2 = new CheckBox("2");
    cb1.selectedProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue) {
            tabPane.getTabs().add(0, tab1);
        } else {
            tabPane.getTabs().remove(tab1);
        }
    });
    cb2.selectedProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue) {
            tabPane.getTabs().add(tab2);
        } else {
            tabPane.getTabs().remove(tab2);
        }
    });

    Scene scene = new Scene(new VBox(new HBox(cb1, cb2), tabPane));

    primaryStage.setScene(scene);
    primaryStage.show();
}

2
这不是一个好的解决方案,因为如果有许多选项卡并且它们被添加和删除,那么在选项卡窗格中控制选项卡索引非常困难。还有其他解决方案吗? - Pavel_K
@Pavel_K 不,要完全隐藏Tab,除了修改列表之外没有其他解决方案。但通常不需要通过索引访问选项卡。如果您确实需要访问“管理索引”等选项卡,则仍可以将所有选项卡存储在不同的List中... - fabian
谢谢您的回答。我的意思是setVisible(false) = remove(tab)setVisible(true) = add(tab)。但是add(tab)会添加到列表的末尾。例如,如果我有12个选项卡,并且想要隐藏第3个选项卡,当它被隐藏后,第2个和第4个选项卡将关闭,那么当我将其添加到第3个位置时,它的位置顺序就会出错。 - Pavel_K

0

虽然这个问题已经很久了,但对于某些人来说可能还是有帮助的。

你可以尝试像这样做。

你有一个包含三个选项卡tabOnetabTwotabThreetabPane

选项卡的位置索引

tabOne - 0
tabTwo - 1
tabThree - 2

要隐藏tabTwo,您可以使用remove函数,要再次显示,可以使用set函数。

要删除选项卡

tabPane.getTabs().remove(tabTwo);

使用相关索引再次设置,以便在正确位置显示。

tabPane.getTabs().set(1, tabTwo);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接