Reputation: 31
I have a Tab with "total" id and get it in a listener with 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;
And 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 handle programm if user click on tab with total id but i used onSelectionChanged that can not help me! How i can handle it if user click on total id tab?
Upvotes: 1
Views: 7081
Reputation: 651
You can use onSelectionChanged
for that, in your Controller event
method, just check if the totalTab is selected or not, like below, please note that the name of the annotated tab must match the fx:id
set in the fxml file, and the method name, in this case event
must match the name onSelectionChanged="#event"
in the fxml file.
public class Controller {
@FXML
private Tab total;
@FXML
void event(Event ev) {
if (total.isSelected()) {
System.out.println("Tab is Selected");
//Do stuff here
}
}
}
or pure programmatically:
total.setOnSelectionChanged(event -> {
if (total.isSelected()) {
System.out.println("Tab is Selected");
//Do stuff here
}
});
Upvotes: 5
Reputation: 41
You can do that with either the selectedItemProperty or selectedIndexProperty . Below is the solution using selectedItemProperty. The switch case is not needed.
// 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());
}
}
});
Upvotes: 4