【问题标题】:how to actually hide Tab from TabPane with JavaFX如何使用 JavaFX 从 TabPane 中实际隐藏 Tab
【发布时间】:2017-06-06 23:19:39
【问题描述】:

以前我正在研究 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 中具有与上述相同的功能,但我不知道该方法是如何工作的,因为没有一种方法能按我的意愿工作。

谁能帮我解决这个问题?

【问题讨论】:

标签: java javafx


【解决方案1】:

只需修改tabs列表:

CheckBoxes 被(取消)选中时,以下示例添加/删除Tabs。

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

【讨论】:

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

问这个问题已经很久了,但这可能对某人有帮助。

你可以试试这样的。

您有一个tabPane,其中包含三个标签tabOnetabTwotabThree

标签的位置索引

tabOne - 0
tabTwo - 1
tabThree - 2

要隐藏tabTwo,可以使用删除功能,再次出现可以使用设置功能。

删除标签

tabPane.getTabs().remove(tabTwo);

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

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

【讨论】:

    猜你喜欢
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多