When I press the red close button, I want the window not to close on JavaFX.
Any suggestions?
From the Javadocs for Window.setOnCloseRequest():
Called when there is an external request to close this Window. The installed event handler can prevent window closing by consuming the received event.
So all you need is
stage.setOnCloseRequest(Event::consume);
or, if you are going to perform other actions as well:
stage.setOnCloseRequest(event -> {
// do some stuff...
event.consume();
});
Related
The real reason I'm trying to do this is to run a game (infinite) loop when the UI shows, but this shows the same problem.
primaryStage.setScene(new Scene(root));
primaryStage.addEventHandler(WindowEvent.WINDOW_SHOWN, event -> {
System.out.println("Shown");
try {
Thread.sleep(4000);
} catch (Exception e) {
e.printStackTrace();
}
});
primaryStage.show();
This code will result in a console message, then a 4 second wait, then the window actually shows. I can't really find anything on this and it doesn't seem to make much sense since the documentation for WINDOW_SHOWN specifically states it fires just after the window is shown.
Not exactly an answer to the original question but a comment as to what solved a problem whose search brought me here.
I had a need to display an APPLICATION_MODAL custom dialog fired off by a MainScene show event hook.
If I displayed the dialog via showAndWait() the parent stage window decorations never completed rendering correctly until the dialog closed.
If I displayed the dialog via show() the parent rendered correctly yet I still had correct modal event-queue blocking that I needed.
Is there a method for a JavaFX controller that gets called when the user leaves that view in the application? The opposite of the initialize() method basically.
I want to do this because I have a page that shows a webcam stream waiting for the user to press a button that takes a snapshot, and I need to close that stream if the user doesn't take a snapshot and leaves the screen. Any other ways of doing this would be appreciated too.
Thanks!
You can use the window's hiding event...
private void detachHook(final Node parent) {
parent.getScene().getWindow()
.addEventFilter(WindowEvent.WINDOW_HIDING, event -> {
//Kill your webcam stream
});
}
You will have to check if you also get the parent as we do in our application, but the window hiding event should work if a node and a scene are present.
I have a task that are hiding a dialog but I need to click the button belong to this dialog to
implement some function before go to the next dialog.
But when I hide this dialog, I can't click the button. Is there any way to implement this button without On_Bn_Clicked() event? I mean that when the dialog is called, the button is also activated.
Thank for the helps.
When you click the button a few Windows messages are sent. The important ones are WM_LBUTTONDOWN, WM_LBUTTONUP which tells the button you clicked the left mouse button down and up. Then some time later a WM_COMMAND message is sent to the parent window to handle the button click. At that point your ON_COMMAND() MFC handler is called. MFC abstracts this all away from you for the most part.
You could go and simulate this using the Win32 SendMessage API but if the message pump is blocking your button may not be clicked when you think it will. If you want a quick answer to your question then this is an approach to "get it done". It would look something like this:
SendMessage(button.GetSafeHwnd(), WM_LBUTTONDOWN, MK_LBUTTON, 0);
SendMessage(button.GetSafeHwnd(), WM_LBUTTONUP, MK_LBUTTON, 0);
I think a more sensible approach is to take the code that is in this On_Bn_Clicked() event handler and simply move it to a reusable function. This way you can call the code in On_Bn_Clicked() from anywhere in your program.
Just call On_Bn_Clicked() directly from your code. There is no harm in doing so. (I suppose you don't want to actually click the hidden button with the mouse...)
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.
Here is the scenario...I have an application that has an "Open" button. When you click the open button, a TitleWindow pops up (via the PopUpManager) a dialog that allows you to select a record. In that window, you can either click "Ok" once you've selected a record, or cancel the dialog which should close the window.
What is the best way to handle this, knowing I need to return the record number to the main application? Right now I have the main application opening up the custom TitleWindow. In the TitleWindow, I have an event that fires when the user clicks "Ok" and a record is selected. That's how the main application listens for the recordId. What I'm wondering is if I can do something like this:
private function RecordSelected():void
{
this.selectedRecord = someControl.selectedIndex;
this.dispatchEvent(new Event("RecordSelected"));
PopUpManager.removePopup(this);
}
Will the instance of the TitleWindow stay active until after the event is handled by the main application, or could it be removed before the main application fully handles the event (thus ending with a null reference exception)?
Alternatively, is it better to close the window in the event handler like so:
private function titleWindow_RecordSelected(event:Event)
{
var openDialog:CustomTitleWindow = CustomTitleWindow(e.currentTarget);
this._selectedRecord = openDialog.selectedRecord;
PopUpManager.removePopup(openDialog);
}
I'm also wondering if sending the selection out in an event is the best way to do this also. Any insight would be appreciated. Thanks in advance.
What is the best way to handle this,
knowing I need to return the record
number to the main application?
Best is always subjective, but I usually handle closing of the window in the window component; and then dispatch an event with appropriate data (In this case Record No) back to the application. It sounds like your 90% there. Just instead of dispatching an event Event, create a custom Event class with your recordID. Then it won't matter if the pop up is still in memory or not when you need to use the record ID.