简体   繁体   中英

How to perform some action when the tab is selected in javafx scene builder?

This is my layout fxml file:

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

I want to perform an action if the user clicks on the tab with total id but I used onSelectionChanged and that can not help me. How I can handle it if user clicks on the total id tab?

Please try the code below;

tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>()
{
    @Override
    public void changed(ObservableValue<? extends Tab> ov, Tab t, Tab t1)
    {
       if ("total".equals(t1.getId()))
       {
        //TODO
       }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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