如何在javafx中选择Tab时执行某些操作?

时间:2017-03-21 12:02:41

标签: javafx fxml scenebuilder

我有一个带"总计" id并使用switch:

在侦听器中获取它
 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如果用户点击带有总ID的标签,但我使用的onSelectionChanged无法帮助我! 如果用户点击总ID标签,我该如何处理?

2 个答案:

答案 0 :(得分:2)

您可以使用onSelectionChanged,在Controller event方法中,只检查是否选择了totalTab,如下所示,请注意带注释的标签名称必须与{ {1}}在fxml文件中设置,方法名称(本例中为fx:id)必须与fxml文件中的名称event匹配。

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

答案 1 :(得分:1)

您可以使用selectedItemProperty或selectedIndexProperty执行此操作。以下是使用selectedItemProperty的解决方案。不需要开关盒。

Parse.User.logIn(USERNAME, PASSWORD).then(
   function success(user){
      (Some codes...)
   },function error(err){
      (print error)
   }
);