簡體   English   中英

如何使用 JavaFX 從 TabPane 中實際隱藏 Tab

[英]how to actually hide Tab from TabPane with JavaFX

以前我正在研究 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 中使用與上面相同的功能,但我不知道這些方法都不是我想要的。

任何人都可以幫助我嗎?

只需修改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();
}

問這個問題已經很久了,但這可能對某人有幫助。

你可以嘗試這樣的事情。

你有一個tabPane有三個選項卡tabOnetabTwotabThree

標簽的位置索引

tabOne - 0
tabTwo - 1
tabThree - 2

要隱藏tabTwo ,您可以使用 remove 功能,再次出現您可以使用 set 功能。

刪除標簽

tabPane.getTabs().remove(tabTwo);

再次設置相關索引以顯示在正確的位置。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM