Reputation: 1561
In my Javafx app, I have a number of tabs in a tab pane. I needed the contents of a tab's display to update when the user clicked on the tab, so I found the below code and used it in the tab's controller:
tabB.setOnSelectionChanged(new EventHandler<Event>() {
@Override
public void handle(Event t) {
refreshTabBData();
}
});
That worked great until I realized the above actually creates two events: the first is when you click on the tab; the second event is when you click on a different tab. I need the refresh to happen only when the target tab is clicked the first time.
Here's my code, reduced to a toy example. First, the "launcher" class:
package tabsAndListeners;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class GoGoGo extends Application {
public void start(Stage primaryStage) {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("TabScreen.fxml"));
AnchorPane root;
try {
root = (AnchorPane) loader.load();
TabScreenController tsc = loader.getController();
tsc.start(primaryStage);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Nothing fancy there. Here's TabScreenController.java:
package tabsAndListeners;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Tab;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class TabScreenController {
@FXML AnchorPane anchorPaneRoot;
@FXML Tab tabA, tabB, tabC;
Stage stage;
int counter = 0;
public void start(Stage stage){
this.stage = stage;
tabB.setOnSelectionChanged(new EventHandler<Event>() {
@Override
public void handle(Event t) {
// refreshTabBData();
System.out.println("tabB.setOnSelectionChanged() :: counter is now: "+counter);
counter++;
}
});
}
}
And finally the FXML, "TabScreen.fxml":
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="anchorPaneRoot" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tabsAndListeners.TabScreenController">
<children>
<TabPane layoutX="10.0" layoutY="70.0" prefHeight="324.0" prefWidth="576.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab fx:id="tabA" text="Tab A">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab fx:id="tabB" text="Tab B">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab fx:id="tabC" text="Tab C">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
So when I run this, and then click TabA, TabB, TabC, TabC, TabB, TabA, TabC. Here's the output:
tabB.setOnSelectionChanged() :: counter is now: 0 // I clicked on TabB
tabB.setOnSelectionChanged() :: counter is now: 1 // I clicked on TabC
tabB.setOnSelectionChanged() :: counter is now: 2 // I clicked on TabB
tabB.setOnSelectionChanged() :: counter is now: 3 // I clicked on TabA
You see the problem. The refreshTabBData() code is called when I click TabB... which I want... but then it is called again when I click another tab, any other tab. I don't want that second event. I want the refresh() code to be called only when TabB is clicked, and that's it.
I've been researching EventHandlers all afternoon, but everything I find it either supergeneralized or superspecific and not relevent to my situation. Can anyone offer any advice on where I am going wrong?
Many thanks!
Upvotes: 1
Views: 1583
Reputation: 209330
Just add an if
statement in the handler:
tabB.setOnSelectionChanged(new EventHandler<Event>() {
@Override
public void handle(Event t) {
if (tabB.isSelected()) {
// refreshTabBData();
System.out.println("tabB.setOnSelectionChanged() :: counter is now: "+counter);
counter++;
}
}
});
Upvotes: 4