Saving last window properties/content in javafx - java

I want to save information from last window (there is possible to use a couple of window in my program) before closing java fx app.
I tried to do this in stop() method, but it saves first opened window.
using Platform.exit() stops whole app after closing randow window.
I tried to do some special main window and let user save chosen window by using extra button, but it's not the prettiest solution.
How can I save last used window? Is there any event handler which is gonna solve my problem?

Yes there is, a few ways you could try...
1) Inside of your Application class, in the Application#launch method, specify the onCloseRequest event
yourStage.setOnCloseRequest(event -> {
//Do Your on close events here
});
2) Inside of your Application class, override the Application#stop method
#Override
public void stop(){
//Do Your on close events here
}
And alternatively, you can specify a system shutdown hook for when the jvm exits, which you can do like so
Runtime.getRuntime().addShutdownHook(() -> whatToDoOnExit());

Related

How to react to getting focus from another application?

My JavaFX 8 application has to doStuff() when it gets focused. That's pretty simple:
primaryStage.focusedProperty().addListener((observable, wasFocused, nowFocused) -> {
doStuff();
});
However, when I display a dialog and user closes it, doStuff() fires. I want it to fire only when user switches from another app to mine, for example Alt+Tab from a browser.
In other words, I want to doStuff() iff other app's window loses focus and my app's window gets focus.
Is it possible?
Edit: Answers posted by FibreFoX and Appelemac require explicitly performing additional step before showing a dialog. This solution is far from perfect, because I (or any developer, in general) have to remember about this step before showing a dialog. Forgetting about it will introduce hard to find bugs.
Extending Dialog to do it automatically isn't an option, because I want to use built-in dialogs that already extend original Dialog.
That's pretty basic feature, I'd be surprised if there's no easy way to achieve this in JavaFX.
You could use a global boolean when opening such dialogs, and only when that global switch is true/false/whatever-you-choose then you could react on that state-switch.
public class GlobalDialogMemory{
public static boolean dialogShown = false;
}
When using CDI you could inject the application-scoped current instance (but you should use getter/setter and non-static booleans instead ;)
I'd suggest adding a listener to your Dialog, which then allows you to not doStuff() if the Dialog was just closed/lost focus.
Easiest way I can think of is setting an Instant (with Instant.now) when the dialog is closed, and if the application regains focus, create another Instant, and check whether the Duration.between(instantLostFocusDialog, instantGainedFocusApp).getSeconds() exceeds 1 (or add getNano() to that to be more specific). Only then would you doStuff()

Close Application UI but keep main() code running

I'm trying to close my main application user interface, but leave code running in my main() function that launched the application. Right now the problem I have is on a Mac the program name remains in Mac's menu bar even though there are no windows shown.
So basically in the code that would exit the application I have:
private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
//System.exit(0);
this.setVisible( false );
// Do something here to finish closing application.
}
The main function that starts the application looks like:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
// NewApplication is a javax.swing.JFrame
new NewApplication().setVisible(true);
}
});
while (true) {
// Watch for user to relaunch UI and do lots of other tasks.
}
}
If I used System.exit(0) it would stop the entire JVM completely and stop running the stuff in the while loop. I cannot figure out how to exit the main application UI, stop from showing in the menu bar, but still run the while loop stuff.
The reason I'm trying to do this is I need something that will run continuously and sometimes the user will need to run a user interface that interacts with the stuff that is running. The stuff inside the while loop checks to see if they are trying to launch the user interface again (among other functions) and would reload it. One option is to make one program that runs continuously and use inter-process communication to talk between the user interface and a non-UI program, but I would need to pass lots of data back and forth so I don't like that option.
It appears there is not an easy way of doing this. For those that have the same problem here are a few options:
1) It looks like other programs I have do this by using Mac’s task bar (in the upper right corner of the screen). The only way you access the program is through a menu on the task bar. Even when you have UI’s shown you get to the UI through the task bar. The downside of doing this is that when the UI is shown you can’t use Cmd+Tab to get over to the window. This is non-intuitive for Mac users. If you want to use this option you can start the java jar file with the command line option “-Dapple.awt.UIElement="true”” and that will prevent the program from showing a menu ALWAYS, and then you'll want to create a task bar icon so the user can get to your program.
See How to hide the Java SWT program icon in the Dock when the application is in the tray
2) Have 2 programs that run, one with a UI and another without. They can communicate using interprocess communication (IPC) using files, sockets, etc. If you don’t have much data to pass between the processes, this is a good solution.
3) You could probably use JNI to remove the menu on the application after all the UI’s close. But you’ll need to dig into Mac’s Objective C language. I can't confirm you can actually do this though.

How can save some Objects, directly after the User has closed the Applications JFrame, but before the Program exits?

Good day!
I'm developping a small Java Application with NetBeans IDE that extends JFrame. I implemented several ways to close the App, for example pressing Ctrl-Q and pressing the X of the JFrame.
But before the actual closing I'd like the program to execute some additional code for me, to save some objects the application needs to reuse the next time it starts up.
What is the best way to change the entire programs closing behavior?
I'm thinking about overriding/replacing the defaultCloseOperation, something like this:
setDefaultCloseOperation( myOwnCloseOperation );
Thanks for any help!
setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE )
Add a WindowListener or WindowAdapter - overriding either the windowClosing() or windowClosed() methods (I forget which) to catch the closing event.
Call the save functionality from the window method.
Call setVisible(false) or dispose() (as discussed in the comments) at the end.

How to trigger an event when another window is closed?

I have a button that open a new window to the user do some configurations. After the configuration window is closed, I want to reload the configurations in the window that called the configuration window.
How I do this?
Use a modal dialog for your configuration window. Then when the dialog is closed execution of the code will continue after your statement that displayed the configuration window so you can reload the properties.
As long as the form isn't freed on close you can still access the variable representing the form and get its properties and control values.
EDIT:
Ok, I'm a little confused, but let's try this again. There are MANY ways in which you can solve this problem.
The easiest way is to simply call the configuration form with ShowModal and then process the configuration information within the button's click event once the form is closed.
Another way is to have the configuration form store its values in an allocated object (a TStringList, for instance) and then send the reference to that object via a message to the main form in the OnClose of the configuration form. Your main form would then use the TStringList to get all the configuration information and then free it. Again, this is only one way of many this can be done.
So much depends on how you want this all to work.
You need to implement a WindowListener. See how to write Window Listeners.
WindowAdapter myListener = new WindowAdapter() {
// maybe you want windowClosing
public void windowClosed(WindowEvent e) {
// actions to perform after window is closed
}
}
// add to a Window (JFrame is a subclass of Window)
myWindow.addWindowListener(this);

Blackberry java app running in background

I would like to exit the application with 2 different ways:
- When key "escape" pressed, the application exit but still run in background.
- When the user select "Close" in the menu to close the application totally. This is already working as it is the default behavior.
In my MainScreen class, I have overwritten the close() method that call super.close() at the end in order to close the screen.
Thank you
See the method Application.requestBackground()
http://www.blackberry.com/developers/docs/4.5.0api/net/rim/device/api/system/Application.html#requestBackground()

Categories

Resources