2016-09-18 128 views
0

我正在通過單擊按鈕從fxml文件動態添加選項卡。是否有方法檢查該選項卡是否已在JavaFX的TabPane中打開並切換到該選項卡,而不是將相同的選項卡添加到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> 

這裏是我的標籤(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> 

回答

3

只要控制器跟蹤您打開了哪個FXML文件(我重構了一些代碼以擺脫所有重複) 。

另外,從來沒有,有史以來初始化@FXML-annotated字段。即永遠不要做@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(); 
      }   
     } 
    } 
} 
+0

非常感謝你@James_D – Kevin

+0

這就是我一直在尋找的。感謝您抽出時間糾正我的錯誤。你很奇怪! – Kevin

相關問題