How to reload an Application in JavaFX? - java

I have a small game which has New Game button. There are many variables which need reset when the New Game button is pressed. Is there any method which can easily reload the whole application or any alternative to refresh the scene, stage or variables? I just want to bring the application to it's initial stage when It's first loaded.
I went through different topics on internet and read many questions and answers here also, but I there is no easy method to implement it. Most of the answers suggest to reuse the whole code or put the whole code in class and reload it.
Questions reviewed:
How to restart a JavaFX application when button is clicked?
How can you reload or refresh a scene with javafx 1.3

I would certainly recommend a clean approach as discussed in the questions you linked.
A quick and dirty way however might be the following:
restartButton.setOnAction( __ ->
{
System.out.println( "Restarting app!" );
primaryStage.close();
Platform.runLater( () -> new ReloadApp().start( new Stage() ) );
} );
Close the main stage, create a new one and pass it to a new instance of your App. I cannot make any guarantees about the memory behavior of this hack. Use it carefully.
Full example: https://gist.github.com/bugabinga/ce9e0ae2328ba34133bd

for reload application you must create new instance of your main class and call
the start method of it with stage parameter.for example
restartButton.setOnAction(e->{yourAPP app=new yourApp();
app.start(yourStage);});

Related

Hiding and showing a javafx stage without losing data

So I wasn't able to find a topic on this, but it is possible to somehow make a JavaFX stage invisible, but not close / hide the stage.
For instance:
stage.hide()
Will essentially close the stage, making it so that if you want to use:
stage.show()
You'd have to reconstruct the entire stage.
To rephrase:
"Is it possible to make a JavaFX stage invisible and then visible again without losing any data?"
After some more searching I came across this topic:
JavaFX: can you create a stage that doesn't show on the task bar and is undecorated?
And as it turns out using:
// Note that this is Kotlin
stage.opacity = 0.0
stage.opacity = 1.0
Will make the stage not visible, but retain its information.
If you keep a reference to the stage's controller, you can close it and make a new stage using the same controller which has all the same information in it.

JavaFX - Remove Element from SwingNode

Suppose I've done this -
SwingNode deviceView = new SwingNode();
JDesktopPane desktop = new JDesktopPane();
// Add elements to desktop
deviceView.setContent(desktop);
Is there a way remove desktop from deviceView? The best way I can come up with is -
deviceView.setContent(new JPanel());
But is there a way to simply remove it?
As noted in the comments I've tried deviceView.setContent(null); which appears to be bugged.
You may try this out and let us know how it goes, although I have not tried it out.
deviceView.getContent().removeAll();
Late to the party but here is what you could try.
deviceView.getContent().removeAll(); /****Note : The UI may not show any updates even if you run it inside the SwingUtilities.invokeLater method */
/**** Now set a new instance that would allow you to set any other content later. At least that is what i have done in one of our Java FX App.*/
deviceView = new SwingNode();

Follow new window after swing action emulated

Using reflection, I've managed to invoke the main method of another java application that uses swing to create windows. I've also been able to grab those windows and manipulate them. When I get to a certain point, I loop through the actionListeners of a certain JMenuItem and I call their actionPerformed telling them the button has been pressed. This works exactly as intended, and opens a new window. When this window opens, however, I want to do something similar to it by first getting the window and then the components inside of it.
However, as soon as the event is fired, the window is created and my program is put in a busy loop waiting for the window I want to interact with to close. This is caused by the application I am invoking, and I have no control over this, nor do I have an opportunity to do anything about it.
Here's how I am doing that event firing
for (ActionListener a : nc.getActionListeners()) {
a.actionPerformed(new ActionEvent(nc, ActionEvent.ACTION_PERFORMED,null) {});
}
What I'm thinking is I might want to have another thread that's looking for the window, but I'm not even sure if that will work..
Window has static method
public static Window[] getWindows() {
return getWindows(AppContext.getAppContext());
}
Frame has similar one
public static Frame[] getFrames()
So you can get copy of the created windows (frames) before your click emulation and compare with the new list after click to find the newly created one.

How to dispose all the jframes and their contents?

I want to have logout functionality in java application, I am using some timers in my application. I want to dispose all the jframes, their contents and stop all timers when i click on logout button. And after pressing logout button the main jframe should disappear and login screen should appear with fresh memory.
I have tried to dispose my jframes and make them visible false and make login screen visible.
All the stuff works well but the previous timers are not stopped and application is using more memory.
In general if you want to start from scratch after logout you can try something like starting new instance of application just before you exit the current instance. In any non trivial program it can get problematic to explicitly cleanup everything yourself. Apart from timers you could have various other kinds of resource leakages.
If you have more than one frame in your application then that's bad design.
However, if you want to dispose your frames then, you might need to get all the frames by Frames.getFrames() and dispose every other frame except your main/required one.
If your model has data then clean it up and clean up other stuff. But that not recommended approach as AKJ suggested for leakages.

Java Swing close ONLY one application

Hi all:
I have a Java Swing App. There is a button allow user to create open up a new window of the application. I use System.Exit(0) when user decides to close the application, however when I press "Close" button, both Application windows closed.
public static void main(String[] args)
{
ghMain = new GreenHouseMain();
}
Above is how I initialize the first application, then use the same code to create new GreenHouseMain Object to open second application window.
So my question is how do I close only one application window which the close button I pressed from?
Thanks all
call dispose() instead of System.exit() on the Window object that you want to close. When there are no more visible windows, the Event Dispatch thread will exit.
I assume that both windows are JFrames. If so, it is better to have the second window be a JDialog, modal or non-modal depending on your requirements. If you need both windows open and want to be able to let the user select which to close, then perhaps both need to be dialogs, though I'm not 100% sure based on the information you've presented. If these suggestions don't solve your problem, then please provide us with more details on your exact requirements.
read the javadocs for setDefaultCloseOperation. System.exit() is doing exactly what it's supposed to, so get rid of it.

Categories