Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to actually hide Tab from TabPane with JavaFX

Tags:

java

javafx

previously I was working on Java Swing and now I'm trying to work with JavaFX. My Java Swing code last time:

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

and in my 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));
    }
}

and in my code:

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;
}

The code above will perform like this:

  1. when the user login with admin account, all the feature (Tab) enabled
  2. when the user login as manager, some feature (tab) will be hide

My current problem now:
I wanted the same feature as above in JavaFX but I don't know how as none of the method works as I wanted.

anyone can help me with this?

like image 381
hokit Avatar asked Jan 22 '17 10:01

hokit


1 Answers

Simply modify the tabs list:

The following example adds/removes Tabs, when the CheckBoxes are (un)selected.

@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();
}
like image 111
fabian Avatar answered Oct 04 '22 15:10

fabian