繁体   English   中英

如何检查JavaFX的TabPane中是否已打开选项卡

[英]How to check if a tab is already opened in TabPane in JavaFX

我通过单击按钮从fxml文件动态添加选项卡。 有没有一种方法可以查看JavaFX的TabPane中是否已打开该选项卡,然后切换到该选项卡,而不是将相同的选项卡添加到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>

这是我的其中一个标签(Tab2)的FXML文件:-

    <?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>

只需让控制器跟踪您打开了哪些FXML文件(我对代码进行了一些重构,以消除所有重复)。

@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();
            }           
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM