Java FX - Progress bar update - java

I have a method that takes a while to complete, when the jar application is started. To have some feedback, i created the form frmWaiting, that displays a simple indeterminate progress bar. I also have a controller for the form,
PrincipalController.
Entry point for the application
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Stage stagePrincipal = new Stage();
Parent parentPrincipal = FXMLLoader.load(getClass().getClassLoader().getResource("frmPrincipal.fxml"));
Scene scenePrincipal = new Scene(parentPrincipal, 300, 275);
stagePrincipal.setScene(scenePrincipal);
stagePrincipal.setHeight(400);
stagePrincipal.setWidth(500);
stagePrincipal.setResizable(false);
stagePrincipal.setTitle("Instalador");
stagePrincipal.show();
} catch (Exception e) {
e.printStackTrace();
}
}
PrincipalController - frmPrincipal.fxml:
#Override
public void initialize(URL location, ResourceBundle resources) {
try {
Stage stageWaiting = new Stage();
Parent parentWaiting;
parentWaiting = FXMLLoader.load(getClass().getClassLoader().getResource("frmWaiting.fxml"));
Scene sceneWaiting = new Scene(parentWaiting, 300, 275);
stageWaiting.setScene(sceneWaiting);
stageWaiting.setHeight(300);
stageWaiting.setWidth(400);
stageWaiting.setResizable(false);
stageWaiting.setTitle("Instalador");
stageWaiting.show();
} catch (IOException e) {
}
}
WaitingController - frmWaiting.xml:
public class WaitingController implements Initializable {
#FXML private ImageView img;
#FXML private ProgressBar progressBar;
#FXML private ProgressIndicator pgIndicator;
private Task copyTask;
#Override
public void initialize(URL location, ResourceBundle resources) {
img.setImage(new Image(getClass().getClassLoader().getResourceAsStream("image.png")));
progressBar.setProgress(ProgressBar.INDETERMINATE_PROGRESS);
ArquivoController.getInstance().copiaArquivosPadrao(); //This is the method that takes a while.
}
public ProgressBar getProgressBar() {
return progressBar;
}
I want to initialize my main form, frmPrincipal, when my method that takes a while finishes. I also want to get the progress bar working. I have tried to do it on another Thread, but i could not get the response from it when the method finishes.
All the .fxml files are correct, ommited them to make things easier if possible.
The way it is, the application waits for the method to finish, then opens the other form. But, the progressBar does not update.

Inside ArquivoController.getInstance().copiaArquivosPadrao(); you have to update the progressBar's progress.
You could do it the nice way, using a Task to run copiaArquivosPadrao(), update the tasks progress accordingly and binding to the tasks progress property.
Or you could do it the ugly way, passing progressBar to copiaArquivosPadrao(), something like this:
public void initialize(URL location, ResourceBundle resources) {
img.setImage(new Image(getClass().getClassLoader().getResourceAsStream("image.png")));
progressBar.setProgress(ProgressBar.INDETERMINATE_PROGRESS);
ArquivoController.getInstance().copiaArquivosPadrao(progressBar); //This is the method that takes a while.
}
and
public void copiaArquivosPadrao(ProgressBar progressBar) {
// call progressBar.setProgress() in here to update the progress bar
// eg.
progressBar.setProgress(0.0F);
doSomething();
progressBar.setProgress(0.20F);
doSomething();
progressBar.setProgress(0.40F);
doSomething();
progressBar.setProgress(0.60F);
doSomething();
progressBar.setProgress(0.80F);
doSomething();
progressBar.setProgress(1.00F);
}
For sure, you can do this more fine-grained in a loop or similar.

Related

JavaFX updating controller property from another controller

I have a progress bar that I am trying to update within javaFX in a controller. I am updating the progress bar based on a function called Generate(), if it is called it should update the progress bar within the main controller. However, the code I have doesn't update it, rather it updates a new instance of the progress bar.
The Generate method in my DrawerContentController is :
try {
AnchorPane ap = fxmlLoader.load();
for(Node node: ap.getChildren()){
if(node.getId().equals("progressBar")){
progressBar = (ProgressBar) node;
progressBar.setProgress(50);
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
In my main controller I have the progressBar set up via fxml using scene builder, i'm trying to update the progressBar from DrawerContentController, which is essentially a sidebar menu that consists of 3 buttons, one of which is generate that calls the Generate() method. I know I should probably be using threads, I am a beginner to JavaFX and still learning on how to use it fully.
I've also tried :
FXMLLoader fxmlLoader = new FXMLLoader((getClass().getResource("layout.fxml")));
and then declaring the controller and instantiating it by
FXMLDocumentController fxmldc = fxmlLoader.getController();
and then assessing the property, however I get a npe this way.
My FXMLDocumentController
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
//Load Splash screen
if (!MainClass.isSplashLoaded)
loadSplashScreen();
//load drawer content
try {
VBox box = FXMLLoader.load(getClass().getResource("drawerContent.fxml"));
drawer.setSidePane(box);
HamburgerBasicCloseTransition transition = new HamburgerBasicCloseTransition(hamburger);
transition.setRate(-1);
hamburger.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
transition.setRate(transition.getRate() * -1);
transition.play();
if (drawer.isShown()) {
drawer.close();
mainText.setVisible(true);
} else {
drawer.open();
mainText.setVisible(false);
}
});
} catch (IOException e1) {
e1.printStackTrace();
}
}
Just create an observable property in DrawerContentController for the progress:
public class DrawerContentController implements Initializable {
private final DoubleProperty progress = new SimpleDoubleProperty();
public DoubleProperty progressProperty() {
return progress ;
}
public final double getProgress() {
return progressProperty().get();
}
public final void setProgress(double progress) {
progressProperty().set(progress);
}
// existing code...
}
Now you can bind your progress bar's progress property to the controller's progress property:
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
//Load Splash screen
if (!MainClass.isSplashLoaded)
loadSplashScreen();
//load drawer content
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("drawerContent.fxml"));
VBox box = loader.load();
DrawerContentController drawerContentController = loader.getController();
progressBar.progressProperty().bind(drawerContentController.progressProperty());
drawer.setSidePane(box);
// ... existing code
}
// ...
}
Now in your DrawerContentController class, if you do this.setProgress(...) (updating the new progress property you defined), it will automatically update the progress bar.

JavaFX: Update ProgressBar (#FXML) from Thread

I want to update a JavaFX ProgressBar defined in an FXML file by another class, initialized in a controller thread. Currently it just does not update.
test.fxml
<ProgressBar fx:id="progressBar" prefWidth="5000.0" progress="0.0">
<VBox.margin>
<Insets top="3.0" />
</VBox.margin>
</ProgressBar>
Controller.java
#FXML
public static ProgressBar progressBar = new ProgressBar(0);
MyMain main;
#FXML
private void handleStartWork() throws Exception {
new Thread() {
#Override
public void run() {
try {
main = new MyMain();
main.doIt();
} catch (final Exception v) {
// ...
}
}
}.start();
}
MyMain.java
public void doIt(){
while(...){
Platform.runLater(() -> PoCOverviewController.progressBar.setProgress((count / sum) * 100));
}
}
I already tried different versions in consideration of posts like:
ProgressBar doesn't work with a fxml file and a controller
How to configure Progress Bar and Progress Indicator of javaFx?
I don't know if it's the right approach to make the ProgressBar static. I just did not want to pass the Object through the workflow.
Update (Xavier Lambros answer):
Now i tried it with singleton but it's still not working:
Controller.java
#FXML
public ProgressBar progressBar = new ProgressBar(0);
private static Controller INSTANCE = new Controller();
public static Controller getInstance() {
return INSTANCE;
}
public ProgressBar getProgressBar() {
return progressBar;
}
MyMain.java
public void doIt(){
while(...){
Platform.runLater(() -> Controller.getInstance().getProgressBar()
.setProgress((count / sum) * 100));
}
}
As noted in javafx 8 compatibility issues - FXML static fields, you cannot make a #FXML-annotated field static (and it makes no sense to do so: these fields are inherently properties of the specific controller instance).
To allow the doIt() method access to the progress bar, you could just pass it directly as a parameter:
#FXML
public ProgressBar progressBar ;
MyMain main;
#FXML
private void handleStartWork() throws Exception {
new Thread() {
#Override
public void run() {
try {
main = new MyMain();
main.doIt(progressBar);
} catch (final Exception v) {
// ...
}
}
}.start();
}
and then
public void doIt(ProgressBar progressBar){
while(...){
Platform.runLater(() -> progressBar.setProgress((count / sum) * 100));
}
}
In some circumstances, it might not make sense for the Main class to have a dependency on the JavaFX API. In that case you could just pass a function that updates the progress bar:
#FXML
public ProgressBar progressBar ;
MyMain main;
#FXML
private void handleStartWork() throws Exception {
new Thread() {
#Override
public void run() {
try {
main = new MyMain();
main.doIt(progressBar::setProgress);
} catch (final Exception v) {
// ...
}
}
}.start();
}
and
public void doIt(DoubleConsumer progressUpdate){
while(...){
Platform.runLater(() -> progressUpdate.accept((count / sum) * 100));
}
}
Note that you haven't shown what's happening in your while loop: if you are submitting too many runnables to the FX Application Thread, you might "flood" it and prevent it from updating in a reasonable time. You might consider using a Task, which has specific API for updating a progress field to which the progress bar's progress property can be bound. If it's still not working, you should edit your question to include a MCVE.
I don't think you can have ProgressBar static.
My way is to have an accessor on the ProgressBar inside your controller and init the controller like this :
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/YourController.fxml");
loader.load();
After, you can access your ProgressBar with :
loader.<YourController>getController().getProgressBar();
If you need, to access it in different classes, many other possibilities, one is to make a Singleton :
public class Singleton
{
private ProgressBar progressBar;
private Singleton()
{}
private static Singleton INSTANCE = new Singleton();
public static Singleton getInstance()
{
return INSTANCE;
}
public ProgressBar getProgressBar() {
return progressBar;
}
public ProgressBar setProgressBar() {
return progressBar;
}
}
To call it :
Singleton.getInstance().getProgressBar();

JavaFX+Afterburner.fx access parent window from child

I have a program with this window structure:
Where left side has buttons and green zone is different windows that change where user clic on a left button.
I have a main class (call Estructura) thats acts as control for main window except green panel. For green panel, I inject (thanks to afterburner.fx DI) correspond panel base on user button selection from left side. Injected panel has its own controller that is lazy thanks to DI Framework base on MVP.
public class EstructuraPresenter implements Initializable {
private static final Logger LOG = getLogger(EstructuraPresenter.class.getName());
#FXML
ToggleButton btnPlantillas, btnAlumnos, btnEstadisticas;
#FXML
BorderPane pEstructura; //Correspond to all window except green zone
#FXML
StackPane pContenedor; //Green zone
//Injection of all child windows that i want to show in green zone
#Inject
private AlumnosView alumnosview;
#Inject
private PlantillasView plantillasview;
#Inject
private EstadisticasView estadisticasview;
#Override
public void initialize(URL url, ResourceBundle rb) {
//I set up green zone with alumnos panel so I call method whit request section to load
cambiarSeccion("Alumnos");
btnPlantillas.setOnAction((ActionEvent event) -> {
cambiarSeccion("Plantillas");
});
btnAlumnos.setOnAction((ActionEvent event) -> {
cambiarSeccion("Alumnos");
});
btnEstadisticas.setOnAction((ActionEvent event) -> {
cambiarSeccion("Estadisticas");
});
}
//I pass to this method, name of window/panel that I like to load in green zone
public void cambiarSeccion(String nombreVentana) {
try {
//First, reset all buttons (so when user select an option and enter in case, I select option button making like effect as a selected)
btnAlumnos.setSelected(false);
btnPlantillas.setSelected(false);
btnEstadisticas.setSelected(false);
switch (nombreVentana) {
case "Alumnos":
if (btnAlumnos.isSelected() == false) {
sepTitulo.setVisible(true);
lbTitulo.setText("Alumnos");
btnAlumnos.setSelected(true);
//I need to do this check because for first time program load because green zone hasn't got any previous panel load
if (pContenedor.getChildren().contains(alumnosview.getView())) {
pContenedor.getChildren().clear();
alumnosview.getViewAsync(pContenedor.getChildren()::add);
} else {
pContenedor.getChildren().add(alumnosview.getView());
}
}
break;
case "Plantillas":
if (btnPlantillas.isSelected() == false) {
sepTitulo.setVisible(true);
lbTitulo.setText("Plantillas");
btnPlantillas.setSelected(true);
pContenedor.getChildren().clear();
plantillasview.getViewAsync(pContenedor.getChildren()::add);
}
break;
case "Estadisticas":
if (btnEstadisticas.isSelected() == false) {
sepTitulo.setVisible(true);
lbTitulo.setText("Estadísticas");
btnEstadisticas.setSelected(true);
pContenedor.getChildren().clear();
estadisticasview.getViewAsync(pContenedor.getChildren()::add);
}
break;
//There are more cases but I use same configuration like above examples...
}
}
catch (Exception e) {
LOG.log(Level.SEVERE, e.toString());
new Dialogos().mostrarExcepcion(null, e);
}
}
}
Well, when user clic on left button, panel is load ok in green zone. But problem I have is where user for example in green zone clic on a button/link... to load another panel (similiar to above description) but in that case I need to call from green panel controller to method cambiarSeccion() that is in EstructuraPresenter controller.
So for example, on case of user select estadisticas button (i hide in image, sorry I tried to simplified), that's is estadisticaspresenter controller:
public class EstadisticasPresenter implements Initializable {
private static final Logger LOG = getLogger(EstadisticasPresenter.class.getName());
#FXML
Hyperlink linkTotalAlumnos;
#Inject
private EstructuraView estructuraview;
#Override
public void initialize(URL url, ResourceBundle rb) {
linkTotalAlumnos.setOnAction((ActionEvent event) -> {
//This call apparently works, I debug and call to method cambiarSeccion happend but screen isn't update
((EstructuraPresenter) estructuraview.getPresenter()).cambiarSeccion("Alumnos");
});
}
}
Where estadisticas window is loaded If I click on left buttons, works but If I clic on green load panel nothing happend. I resume problem in next image:
In my opinion your approach has one problem: When you call in any of your children presenters
((EstructuraPresenter) estructuraview.getPresenter())
.cambiarSeccion("Alumnos");
estructuraview.getPresenter() is actually creating a new instance of the main presenter.
This means that EstructuraPresenter.initialize() is being called again when you click on the hyperlink, and you have two calls to cambiarSeccion() (one from Initialize, one from the action), that add the children to the new instance, not to the old one, which is the one you see. That's why you don't see any change!
I'd suggest a different approach: Let the main presenter listen to changes in some properties on their childrens.
For example, add a boolean property to notify a click on the hyperlink:
public class EstadisticasPresenter implements Initializable {
#FXML Hyperlink linkTotalAlumnos;
private final BooleanProperty link = new SimpleBooleanProperty();
public boolean isLink() { return link.get(); }
public void setLink(boolean value) { link.set(value); }
public BooleanProperty linkProperty() { return link; }
#Override public void initialize(URL url, ResourceBundle rb) {
linkTotalAlumnos.setOnAction((ActionEvent event) -> {
link.set(true);
});
}
}
while on the main presenter:
#Override
public void initialize(URL url, ResourceBundle rb) {
...
EstadisticasPresenter estadisticas =
(EstadisticasPresenter)estadisticasview.getPresenter();
estadisticas.linkProperty().addListener((ob,b,b1)->{
if(b1){
cambiarSeccion("Alumnos");
// reset link property
estadisticas.setLink(false);
}
});
}
As a side note, instead of clear() and add, I'd use pContenedor.getChildren().setAll(<view>); since you only show one child at a time.
Base on #josé-pereda answer, finally I can link button/hyperlink from sub-window (green area) to main controller. The advantage of my solution is that I avoid use of boolean variables.
In EstructuraPresenter controller I add listeners to buttons from EstadisticasPresenter(which is the controller from one of the sub-window):
public class EstructuraPresenter implements Initializable {
private static final Logger LOG = getLogger(EstructuraPresenter.class.getName());
#FXML
ToggleButton btnAlumnos, btnEstadisticas;
#FXML
StackPane pContenedor;
#Inject
private AlumnosView alumnosview;
#Inject
private EstadisticasView estadisticasview;
#Inject
private DataModel datos;
#Override
public void initialize(URL url, ResourceBundle rb) {
((EstadisticasPresenter) estadisticasview.getPresenter()).getLinkTotalAlumnos().setOnAction((ActionEvent event) -> {
this.cambiarSeccion("Alumnos");
});
//This call is from first time, where program starts to show by default Alumnos sub-window
cambiarSeccion("Alumnos");
btnAlumnos.setOnAction((ActionEvent event) -> {
cambiarSeccion("Alumnos");
});
btnEstadisticas.setOnAction((ActionEvent event) -> {
cambiarSeccion("Estadisticas");
});
}
public void cambiarSeccion(String nombreVentana) {
try {
btnAlumnos.setSelected(false);
btnEstadisticas.setSelected(false);
switch (nombreVentana) {
case "Alumnos":
if (btnAlumnos.isSelected() == false) {
sepTitulo.setVisible(true);
lbTitulo.setText("Alumnos");
btnAlumnos.setSelected(true);
alumnosview.getViewAsync(pContenedor.getChildren()::setAll);
pContenedor.getChildren().setAll(alumnosview.getView());
}
break;
case "Estadisticas":
if (btnEstadisticas.isSelected() == false) {
sepTitulo.setVisible(true);
lbTitulo.setText("Estadísticas");
btnEstadisticas.setSelected(true);
estadisticasview.getViewAsync(pContenedor.getChildren()::setAll);
}
break;
}
}
catch (Exception e) {
LOG.log(Level.SEVERE, e.toString());
new Dialogos().mostrarExcepcion(null, e);
}
}
}
NOTE In addition, I change getChildren().clear() && getChildren().add() to getChildren().setAll(); These prevents re-initializate controllers from each window that I put in green area.
estadisticasview.getViewAsync(pContenedor.getChildren()::setAll);
And in EstadisticasPresenter controller, I add getters from that controls that I want to user clic to go to another window (so when user clic on control, green area change with another window/pane).
public class EstadisticasPresenter implements Initializable {
private static final Logger LOG = getLogger(EstadisticasPresenter.class.getName());
#FXML
Hyperlink linkTotalAlumnos;
#Inject
private DataModel datamodel;
#Override
public void initialize(URL url, ResourceBundle rb) {
}
//Getters that I use in EstructuraPresenter controller to set up event handlers
public Hyperlink getLinkTotalAlumnos() {
return linkTotalAlumnos;
}
}

JavaFX integration with Spring rest client [duplicate]

How to call the launch() more than once in java i am given an exception as "ERROR IN MAIN:java.lang.IllegalStateException: Application launch must not be called more than once"
I have create rest cleint in my java application when request comes it call javafx and opening webview after completing webview operarion am closing javafx windows using Platform.exit() method. when second request comes am getting this error how to reslove this error.
JavaFx Application Code:
public class AppWebview extends Application {
public static Stage stage;
#Override
public void start(Stage _stage) throws Exception {
stage = _stage;
StackPane root = new StackPane();
WebView view = new WebView();
WebEngine engine = view.getEngine();
engine.load(PaymentServerRestAPI.BROWSER_URL);
root.getChildren().add(view);
engine.setJavaScriptEnabled(true);
Scene scene = new Scene(root, 800, 600);
stage.setScene(scene);
engine.setOnResized(new EventHandler<WebEvent<Rectangle2D>>() {
public void handle(WebEvent<Rectangle2D> ev) {
Rectangle2D r = ev.getData();
stage.setWidth(r.getWidth());
stage.setHeight(r.getHeight());
}
});
JSObject window = (JSObject) engine.executeScript("window");
window.setMember("app", new BrowserApp());
stage.show();
}
public static void main(String[] args) {
launch(args);
}
RestClient Method:
Calling to JavaFX application
// method 1 to lanch javafx
javafx.application.Application.launch(AppWebview.class);
// method 2 to lanch javafx
String[] arguments = new String[] {"123"};
AppWebview .main(arguments);
You can't call launch() on a JavaFX application more than once, it's not allowed.
From the javadoc:
It must not be called more than once or an exception will be thrown.
Suggestion for showing a window periodically
Just call Application.launch() once.
Keep the JavaFX runtime running in the background using Platform.setImplicitExit(false), so that JavaFX does not shutdown automatically when you hide the last application window.
The next time you need another window, wrap the window show() call in Platform.runLater(), so that the call gets executed on the JavaFX application thread.
For a short summary implementation of this approach:
See the answer by sergioFC
If you are mixing Swing you can use a JFXPanel instead of an Application, but the usage pattern will be similar to that outlined above.
For an example of the JFXPanel apprach, see Irshad Babar
s answer.
Wumpus Sample
This example is bit more complicated than it needs to be because it also involves timer tasks. However it does provide a complete stand-alone example, which might help sometimes.
import javafx.animation.PauseTransition;
import javafx.application.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.*;
// hunt the Wumpus....
public class Wumpus extends Application {
private static final Insets SAFETY_ZONE = new Insets(10);
private Label cowerInFear = new Label();
private Stage mainStage;
#Override
public void start(final Stage stage) {
// wumpus rulez
mainStage = stage;
mainStage.setAlwaysOnTop(true);
// the wumpus doesn't leave when the last stage is hidden.
Platform.setImplicitExit(false);
// the savage Wumpus will attack
// in the background when we least expect
// (at regular intervals ;-).
Timer timer = new Timer();
timer.schedule(new WumpusAttack(), 0, 5_000);
// every time we cower in fear
// from the last savage attack
// the wumpus will hide two seconds later.
cowerInFear.setPadding(SAFETY_ZONE);
cowerInFear.textProperty().addListener((observable, oldValue, newValue) -> {
PauseTransition pause = new PauseTransition(
Duration.seconds(2)
);
pause.setOnFinished(event -> stage.hide());
pause.play();
});
// when we just can't take it anymore,
// a simple click will quiet the Wumpus,
// but you have to be quick...
cowerInFear.setOnMouseClicked(event -> {
timer.cancel();
Platform.exit();
});
stage.setScene(new Scene(cowerInFear));
}
// it's so scary...
public class WumpusAttack extends TimerTask {
private String[] attacks = {
"hugs you",
"reads you a bedtime story",
"sings you a lullaby",
"puts you to sleep"
};
// the restaurant at the end of the universe.
private Random random = new Random(42);
#Override
public void run() {
// use runlater when we mess with the scene graph,
// so we don't cross the streams, as that would be bad.
Platform.runLater(() -> {
cowerInFear.setText("The Wumpus " + nextAttack() + "!");
mainStage.sizeToScene();
mainStage.show();
});
}
private String nextAttack() {
return attacks[random.nextInt(attacks.length)];
}
}
public static void main(String[] args) {
launch(args);
}
}
Update, Jan 2020
Java 9 added a new feature called Platform.startup(), which you can use to trigger startup of the JavaFX runtime without defining a class derived from Application and calling launch() on it. Platform.startup() has similar restrictions to the launch() method (you cannot call Platform.startup() more than once), so the elements of how it can be applied is similar to the launch() discussion and Wumpus example in this answer.
For a demonstration on how Platform.startup() can be used, see Fabian's answer to How to achieve JavaFX and non-JavaFX interaction?
I use something like this, similar to other answers.
private static volatile boolean javaFxLaunched = false;
public static void myLaunch(Class<? extends Application> applicationClass) {
if (!javaFxLaunched) { // First time
Platform.setImplicitExit(false);
new Thread(()->Application.launch(applicationClass)).start();
javaFxLaunched = true;
} else { // Next times
Platform.runLater(()->{
try {
Application application = applicationClass.newInstance();
Stage primaryStage = new Stage();
application.start(primaryStage);
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
try this, I tried this and found successful
#Override
public void start() {
super.start();
try {
// Because we need to init the JavaFX toolkit - which usually Application.launch does
// I'm not sure if this way of launching has any effect on anything
new JFXPanel();
Platform.runLater(new Runnable() {
#Override
public void run() {
// Your class that extends Application
new ArtisanArmourerInterface().start(new Stage());
}
});
} catch (Exception e) {
e.printStackTrace();
}
}

JavaFX 2 Window event handling in controllers

So I am trying to handle WINDOW_SHOWN event from my controller with code like this:
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
initializeDatePickers();
System.out.println("payer number in initialize: " + payerNumber);
URL location = getClass().getResource("/createUser.fxml");
FXMLLoader loader = new FXMLLoader();
try {
Parent root = (Parent) loader.load(location.openStream());
root.getScene().getWindow().setOnShown(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent event) {
System.out.println("ONSHOWN");
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
But all I've got was endless cycle and program crash.
The code below didn't work either, it returns NullPointerException:
#FXML private AnchorPane createUserDialog; //my root pane
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
createUserDialog.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_SHOWN,
new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent window) {
System.out.println("ONSHOWN");
}
});
}
Implementing WindowEvent interface didn't work at all, don't know why.
So, how could I handle this event? And why I've got NullPointerException? In docs said that initialize() calling only after root pane completely processed.
When the initialize() method is being executed, the root pane is completely constructed but is not added to a scene, or a window. (The initialize() method is executed as part of the execution of your FXMLLoader's load() method; check the code where you call that and you will see that you add the root to a scene and place it in a window after that.) So during the execution of intialize(), root.getScene() will return null.
You can use a Binding to check when the window changes and attach a listener to it:
final EventHandler<WindowEvent> shownHandler = new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent event) {
System.out.println("Shown");
}
};
Bindings.<Window>select(createUserDialog.sceneProperty(), "window").addListener(new ChangeListener<Window>() {
#Override
public void changed(ObservableValue<? extends Window> observable,
Window oldValue, Window newValue) {
if (oldValue != null) {
oldValue.removeEventHandler(WindowEvent.WINDOW_SHOWN, shownHandler);
}
if (newValue != null) {
newValue.addEventHandler(WindowEvent.WINDOW_SHOWN, shownHandler);
}
}
});
This code assumes the root is only ever added to one window; in the unlikely event you're taking the root out of one window and putting it in another during your application life cycle, you would need to remove the listener from the old window. If you need this I'll update the code, but it makes it more complex.

Categories