Как проверить, открыта ли уже вкладка в TabPane в JavaFX

Я динамически добавляю вкладки из файлов fxml, нажав кнопку. Есть ли способ проверить, открыта ли эта вкладка в TabPane в JavaFX, и переключиться на эту вкладку вместо добавления той же вкладки в TabPane.

Вот мой класс контроллера: -

    package application;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.AnchorPane;

public class MainController implements Initializable{

    @FXML
    public TabPane myTabPane ;
    public AnchorPane myAnchorPane;

    @FXML
    public Button btnTab1 = new Button();
    @FXML
    public Button btnTab2 = new Button();
    @FXML
    public Button btnTab3 = new Button();

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        btnTab1.setOnAction(e -> {
            //System.out.println("Clicked");            
            try {
                Tab myNewTab = FXMLLoader.load(this.getClass().getResource("MyTestTab.fxml"));
                myTabPane.getTabs().add(myNewTab);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        });

        btnTab2.setOnAction(e -> {
            try {               
                Tab myNewTab = FXMLLoader.load(this.getClass().getResource("MyTestTab2.fxml"));
                myTabPane.getTabs().add(myNewTab);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }           
        });
    }
}

Вот мой основной файл FXML: -

    <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <top>
      <ToolBar prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
        <items>
          <Button fx:id="btnTab1" mnemonicParsing="false" text="Tab1" />
            <Button fx:id="btnTab2" mnemonicParsing="false" text="Tab2" />
            <Button fx:id="btnTab3" mnemonicParsing="false" text="Tab3" />
        </items>
      </ToolBar>
   </top>
   <center>
      <SplitPane dividerPositions="0.29797979797979796" prefHeight="160.0" prefWidth="200.0" BorderPane.alignment="CENTER">
        <items>
          <AnchorPane fx:id="myTabAnchPane" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
               <children>
                  <TabPane fx:id="myTabPane" layoutX="-12.0" layoutY="34.0" prefHeight="358.0" prefWidth="175.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
               </children></AnchorPane>
          <AnchorPane fx:id="myAnchorPane" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
        </items>
      </SplitPane>
   </center>
</BorderPane>

А вот мой файл FXML для одной из вкладок (Tab2): -

    <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>

<Tab fx:id="tab2" text="My Profile" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
         <content>
            <VBox prefHeight="200.0" prefWidth="100.0">
               <children>
                  <HBox spacing="10.0">
                     <children>
                        <Label text="Bank Statement File" />
                        <TextField prefHeight="25.0" prefWidth="288.0" />
                        <Button mnemonicParsing="false" text="Browse" />
                     </children>
                     <VBox.margin>
                        <Insets left="20.0" top="20.0" />
                     </VBox.margin>
                  </HBox>
                  <HBox VBox.vgrow="ALWAYS">
                     <children>
                        <Pane prefHeight="200.0" prefWidth="135.0" />
                        <TextArea HBox.hgrow="ALWAYS" />
                        <Pane prefHeight="200.0" prefWidth="200.0" />
                     </children>
                     <padding>
                        <Insets top="50.0" />
                     </padding>
                  </HBox>
                  <Pane prefHeight="64.0" prefWidth="600.0" />
               </children>
            </VBox>
         </content></Tab>

person Kevin    schedule 18.09.2016    source источник


Ответы (1)


Просто пусть контроллер отслеживает, какие файлы FXML вы открыли (я немного изменил код, чтобы избавиться от повторений).

Кроме того, никогда не инициализируйте @FXML поля с аннотациями. т.е. никогда не делайте @FXML private Button btnTab1 = new Button();.

package application;

import java.io.IOException;
import java.net.URL;
import java.util.Map ;
import java.util.HashMap ;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.AnchorPane;

public class MainController implements Initializable{

    private Map<String, Tab> openTabs = new HashMap<>();

    @FXML
    private TabPane myTabPane ;
    @FXML
    private AnchorPane myAnchorPane;

    @FXML
    private Button btnTab1 ;
    @FXML
    private Button btnTab2 ;
    @FXML
    private Button btnTab3 ;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        btnTab1.setOnAction(e -> openTab("MyTestTab.fxml"));
        btnTab2.setOnAction(e -> openTab("MyTestTab2.fxml"));
    }

    private void openTab(String fxmlFile) {
        if (openTabs.containsKey(fxmlFile)) {
            myTabPane.getSelectionModel().select(openTabs.get(fxmlFile));
        } else {
            try {               
                Tab myNewTab = FXMLLoader.load(this.getClass().getResource(fxmlFile));
                myTabPane.getTabs().add(myNewTab);
                openTabs.put(fxmlFile, myNewTab);
                myNewTab.setOnClosed(e -> openTabs.remove(fxmlFile));
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }           
        }
    }
}
person James_D    schedule 18.09.2016
comment
Это именно то, что я искал. Спасибо, что нашли время, чтобы исправить мои ошибки, а также. Вы фантастичны!!!! - person Kevin; 19.09.2016