Run the code.
Click Tab2 once.
Output: "Changed".
All fine so far.
====================
Then click on Tab2 again.
Output: "Changed Changed"
In the first event Tab2 is selected==false, then it is selected==true.
So it seems a click on an already selected tab deselects it first, and then selects it again.
There shouldn't be an event at all (nothing has changed, Tab2 was already selected), but instead TWO events are fired!
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TestApp3 extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(final Stage stage) throws Exception {
final VBox root = new VBox();
TabPane tabPane = new TabPane();
Tab tab1 = new Tab("Test1");
Tab tab2 = new Tab("Test2");
tabPane.getTabs().add(tab1);
tabPane.getTabs().add(tab2);
tab2.setOnSelectionChanged(new EventHandler<Event>() {
@Override
public void handle(Event event) {
System.out.println("Changed");
}
});
root.getChildren().addAll(tabPane);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
Click Tab2 once.
Output: "Changed".
All fine so far.
====================
Then click on Tab2 again.
Output: "Changed Changed"
In the first event Tab2 is selected==false, then it is selected==true.
So it seems a click on an already selected tab deselects it first, and then selects it again.
There shouldn't be an event at all (nothing has changed, Tab2 was already selected), but instead TWO events are fired!
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TestApp3 extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(final Stage stage) throws Exception {
final VBox root = new VBox();
TabPane tabPane = new TabPane();
Tab tab1 = new Tab("Test1");
Tab tab2 = new Tab("Test2");
tabPane.getTabs().add(tab1);
tabPane.getTabs().add(tab2);
tab2.setOnSelectionChanged(new EventHandler<Event>() {
@Override
public void handle(Event event) {
System.out.println("Changed");
}
});
root.getChildren().addAll(tabPane);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}