James Fang
James Fang

Reputation: 35

JavaFx TabPane Automatic Closing

I have a MenuItem "load" that opens four tabs in the GUI. However, when I click "load" again. Instead of closing the previous tabs opened. It simply appends after it. How can I close them automatically?

load.setOnAction(e -> {
        cb.loadBinary();
        pb.loadPersonBinary();
        tb.loadTextbookBinary();

        Tab tabS = new Tab();
        tabS.setText("Student");
        studentScene = new SceneStudent(this, pb);
        tabS.setContent(studentScene.getPane());
        root.setCenter(tabPane);

        Tab tabF = new Tab();
        tabF.setText("Faculty");
        facultyScene = new SceneFaculty(this, pb);
        tabF.setContent(facultyScene.getPane());
        root.setCenter(tabPane);

        Tab tab = new Tab();
        tab.setText("Text Book");
        textScene = new SceneTextBook(this, tb);
        tab.setContent(textScene.getPane());
        root.setCenter(tabPane);

        Tab tabC = new Tab();
        tabC.setText("Course");
        courseScene = new SceneCourse(this, cb);
        tabC.setContent(courseScene.getPane());

        tabPane.setTabClosingPolicy(TabClosingPolicy.ALL_TABS);

        MenuItem buttonClicked = (MenuItem) e.getTarget();
        if(buttonClicked == load){
            tabPane.getTabs().removeAll();
        }

        tabPane.getTabs().addAll(tabS, tabF, tab, tabC);
        root.setCenter(tabPane);
    });

Upvotes: 1

Views: 115

Answers (1)

ashhappens
ashhappens

Reputation: 133

Instead of checking if the MenuItem that was clicked was your "load" item, removing, then adding, just do: tabPane.getTabs().setAll(tabS, tabF, tab, tabC);

See documentation here.

Upvotes: 1

Related Questions