Reputation: 13
I have tabpane and depending upon what is going on I'd like to make certain tabs invisible and others visible. The scene and tabs are all defined in FXML.
I know I can use:
tabPane.getTabs().add(0, tab1);
and
tabPane.getTabs().remove(tab1);
but all my tabs, including tab1 are defined in FXML. If I could get and save all the defined tabs and save them so I could re-add them later that would be OK.
Upvotes: 1
Views: 2679
Reputation: 4209
Your solution above is fine. Just make sure that in your FXML you give an ID to each tab you want to manipulate.
<Tab fx:id="myTab1">....</Tab>
For this example I used the closing policy of the tab control to remove tabs from the scene. But this should give you enough information to start with. Here is a working solution that might point you in the right direction:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="250.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.TestController">
<children>
<TabPane fx:id="tabPane" layoutX="14.0" layoutY="14.0" prefHeight="294.0" prefWidth="446.0" AnchorPane.bottomAnchor="30.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab fx:id="tab1" text="Tab 1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab fx:id="tab2" text="Tab 2">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab fx:id="tab3" text="Tab 3">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab fx:id="tab4" text="Tab 4">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
<ButtonBar layoutX="138.0" layoutY="216.0" prefHeight="40.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0">
<buttons>
<Button mnemonicParsing="false" onAction="#saveTabs" text="Save" />
<Button mnemonicParsing="false" onAction="#openTabs" text="Open" />
</buttons>
</ButtonBar>
</children>
</AnchorPane>
Main Class:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader();
Parent node = loader.load(Main.class.getClassLoader().getResource("fxml/TestFXML.fxml").openStream());
Scene scene = new Scene(node,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Controller:
package application;
import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;
import javafx.fxml.FXML;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
public class TestController {
@FXML
private TabPane tabPane;
@FXML
private Tab tab1, tab2, tab3, tab4;
Collection<Tab> tabs = new ArrayList<>();
Collection<String> openTabIds = new ArrayList<>();
@FXML
private void initialize() {
tabs.add(tab1);
tabs.add(tab2);
tabs.add(tab3);
tabs.add(tab4);
}
@FXML
void openTabs() {
openTabIds.stream().forEach(string -> {
tabs.stream()
.filter(tab -> tab.getId().equals(string)).findFirst()
.ifPresent(tab -> tabPane.getTabs().add(tab));
});
}
@FXML
void saveTabs() {
openTabIds = tabPane.getTabs().stream().map(tab -> tab.getId()).collect(Collectors.toList());
}
}
Upvotes: 1