2017-03-21 25 views
0

我有一個「總」 ID的標籤,並把它與交換機監聽器:如何在javafx中選擇Tab時執行一些操作?

switch ((event.getSource().toString().substring(event.getSource().toString().indexOf("=")+1,event.getSource().toString().indexOf(",")))) { 
     case "signin": 
      changeScene(signin, "atmOperation"); 
      User user = new User(); 
      user.check(); 
      break; 

     case "signout": 
      changeScene(signout, "login"); 
      break; 
     case "signup": 
      changeScene(signup, "signup"); 
      break; 
     case "back": 
      changeScene(back, "atmOperation"); 
      break; 
     case "exit": 
      stage = (Stage) exit.getScene().getWindow(); 
      stage.close(); 
      break; 
     case "total": 
      System.out.print(total.isSelected()); 
      break; 

這是我的佈局FXML文件:

<?import javafx.scene.control.Tab?> 
<?import javafx.scene.control.TabPane?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.layout.Pane?> 

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="443.0" prefWidth="610.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> 
    <children> 
     <TabPane fx:id="tabPane" layoutY="37.0" nodeOrientation="RIGHT_TO_LEFT" onContextMenuRequested="#event" prefHeight="400.0" prefWidth="610.0" tabClosingPolicy="UNAVAILABLE"> 
     <tabs> 
      <Tab fx:id="total" closable="false" text="x" onSelectionChanged="#event"> 
      <content> 
       <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" /> 
      </content> 
      </Tab> 
      <Tab text="x"> 
      <content> 
       <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" /> 
      </content> 
      </Tab> 
      <Tab text="y"> 
       <content> 
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" /> 
       </content> 
      </Tab> 
      <Tab text="y"> 
       <content> 
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" /> 
       </content> 
      </Tab> 
     </tabs> 
     </TabPane> 
    </children> 
</Pane> 

我要處理,如果PROGRAMM用戶點擊與總標識的選項卡上,但我用SelectionChanged,不能幫助我! 如果用戶點擊全部ID選項卡,我可以如何處理它?

+0

你想要的選項卡與FX當執行一些動作:選擇ID'total'?或者你是否希望Tab應該被默認選中? – ItachiUchiha

+0

謝謝我的朋友,我想在選中fx:id總數的Tab時執行一些操作 –

回答

0

您可以使用selectedItemProperty或selectedIndexProperty來做到這一點。以下是使用selectedItemProperty的解決方案。開關箱不是必需的。

// starts here 
tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() { 
    @Override 
    public void changed(ObservableValue<? extends Tab> observable, Tab oldTab, Tab newTab) { 
     if(newTab.equals (total)) {    
      System.out.print(total.isSelected()); 
     } 
    } 
}); 
0

您可以使用onSelectionChanged的是,在你的控制器event方法,如果totalTab選擇或沒有,像下面只是檢查,請注意,註釋標籤的名稱必須與FXML的fx:id集相匹配文件和方法名稱,在這種情況下,event必須與fxml文件中的名稱onSelectionChanged="#event"相匹配。

public class Controller { 

     @FXML 
     private Tab total; 

     @FXML 
     void event(Event ev) { 
      if (total.isSelected()) { 
       System.out.println("Tab is Selected"); 
       //Do stuff here 
      } 
     } 
    } 

或純編程:

total.setOnSelectionChanged(event -> { 
     if (total.isSelected()) { 
      System.out.println("Tab is Selected"); 
      //Do stuff here 
     } 
    }); 
相關問題