Javafx Hide Show [duplicate] - javafx

I need to creat an app that shows the primary stage to the user, and if the user closes the stage, instead of finishing the application, it should just hide the stage for later use.
On swing we could just call setVisible(false) to a JFrame or JDialog, but how to do it on JavaFX?

Once the JavaFX toolkit has started, by default it will close down when the last visible window is closed.
To prevent this, you can call
Platform.setImplicitExit(false);
You typically do this in your start(...) method, though it can be called from any thread.
To exit the application, you would then need to call
Platform.exit();
(as the application no longer exits automatically).

Related

Switching To Primary Scene When Secondary Scene Is Closed

I am using JavaFX 11 and a newbie.
I have a single stage with two scenes: a primary scene that shows on start and a secondary scene that is switched to and shown when I press a certain button on the main scene. On the secondary scene, I want to be able to switch back to the main scene when I click the close X button on the top right of the window instead of having the entire application close.
I currently have a method for the cancel button that looks like this:
public void cancelButtonPushed(ActionEvent event) throws IOException {
Parent parent = FXMLLoader.load(getClass().getResource("ExampleMainScreen.fxml"));
Scene scene = new Scene(parent);
Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
window.setScene(scene);
window.show();
}
This method allows me to switch back to the main scene when the Cancel button is pushed. However, I am lost trying to find something that can be used any time the user clicks the close X on the secondary scene.
First, get the terminology right, my guess is that you have two Stages. Scenes can be displayed inside those.
Second, Scene Builder (and the FXML it produces) does not manage stages, it only constructs nodes (and event handling for those nodes) that are placed inside scenes. So, you won't find the hooks you need to integrate with the window close functions in SceneBuilder or FXML.
Third, when a user wants to close a window (a stage is a kind of window), then an event will be emitted, which you can action onCloseRequest.
Fourth, somehow you have already managed to create a second stage, probably by calling new Stage(). This will provide you with a reference to the stage which you can set your close request on:
Stage secondaryStage = new Stage();
Stage setScene(secondaryScene);
secondaryStage.setOnCloseRequest(e -> primaryStage.show());
This will show your primary stage (which I guess you hid earlier), when the secondary stage is being closed, but before it has actually closed.
Next, read up on the Application lifecycle, specifically see the section which references Platform.setImplicitExit(boolean implicitExit):
If this attribute is true, the JavaFX runtime will implicitly shutdown when the last window is closed; the JavaFX launcher will call the Application.stop() method and terminate the JavaFX application thread. If this attribute is false, the application will continue to run normally even after the last window is closed, until the application calls exit(). The default value is true.
Note, that, you probably don't need to explicitly set the implicit exit flag if you handle the stage switching as outlined previously, but I provide the info for you in case you need to understand it.
Finally, consider whether you really should be creating new stages for your application and this particular task or just replacing the content in a single stage (similar to how a web browser works).

QtQuick lower() and raise() does not work while close() works properly

I have 5 windows which i push and pop via a stackview. In the emulator the new window called by push gets on top as it should. However on my android it spawns behind the starting window. I tried using lower() to set the starting window behind the new window with no success
//start page
Button{myStack.push(page_2); lower()}
I also tried rasing the new window on completion with no success
//new page
Component.OnCompleted:raise()
However close() works properly closing the start page and thus making the new page visible.
//start page
Button{myStack.push(page_2); close()}
Qt doc goes as follows for lower()
lower()
Lowers the window in the windowing system.
Requests that the window be lowered to appear below other windows.
and for raise()
raise()
Raises the window in the windowing system.
Requests that the window be raised to appear above other windows.
Is my code correct? why does close() work and the others dont't
First of all, I don't think that Qt supports multi window on Android. One single window only.
Second - what you posted is not even valid QML code.
Third - in that case you use the the stack view within a single window, and push different GUI elements onto it. Refer to the documentation.

prevent QApplication::exec from blocking main thread

I have a visual c++ program which creates multiple GUI on the main thread. I want to show a QWidget alongside all the other GUI. Currently, if I call QApplication.exec(), it blocks the main thread until I close the window. Is there any way to prevent the exec function from blocking the main thread or to use QWidget without calling exec?
The method is not blocking the main thread, on the contrary: it allows the event loop to execute, ensuring that the UI remains responsive.
While the widget is shown, all the other GUI will be responsive, as Qt's event loop fully interoperates with the native message queue.
If you want something to happen when a dialog widget gets closed, connect the relevant code to e.g. the dialog's accepted() signal.

Do javafx operations before quit application

I would like to open a window with a progress bar when I quit my application while some operations run.
I tried to put my code in the stop method from the Application class but javafx is already down at this moment.
I did the same thing in the first windows like this:
primaryStage.setOnCloseRequest(event -> {
Stage loading = new Stage();
loading.initModality(Modality.WINDOW_MODAL);
loading.initOwner(primaryStage);
loading.setScene(new Scene(new Group(JfxUtils.loadFxml(new FXMLLoader(), FxmlFileConstantes.LOADING))));
loading.show();
});
But same thing, it doesn't work.
How can I do this ?
Thanks.
Consume the close request event in your onCloseRequest event handler.
event.consume();
This will stop the window from closing, which, if the window is the last window in the application, would trigger an application shutdown.
Alternately, you can set Platform.setImplicitExit(false), in which case "the application will continue to run normally even after the last window is closed, until the application calls exit()".
The JavaFX application lifecycle is defined in the javadoc of the Application class, which you should probably read to get some more background information.

QT Application with Dialogs

I am writing an application in qt. This application will have many dialogs. My question is: what is the strategy with dialog handling? Should I create them in the constructor of the main window or shouold they be created when the user click on buttons (that is when the user need it). Should they be destroyed or they are automatically destroyed?
There is no hard rule to this. Generally dialogs are small and light, and therefore are created when opened and destroyed (usually automatically) when closed. However, if you have a custom dialog that contains very heavy widgets and/or needs to get data from a slow source, then you can create a dialog and only show when it when required.
Should they be destroyed or they are automatically destroyed?
This depends entirely on how you create it. The best I can do is point you to the most informative source.

Resources