How to trap the [x] in closing a Shell - java

I have the following Shell listener code
private class ShellListener extends ShellAdapter
{
#Override
public void shellClosed( ShellEvent e )
{
}
#Override
public void shellDeactivated( ShellEvent e )
{
}
}
I need to be able to trap when the Shell loses focus, ie: the user goes to another application. The shellDeactivated() does that. I also need to know when I explicitly close() the Shell. The shellClosed() does that.
However when a user clicks on the [x] icon at the top/right corner of the Shell, shellDeactivated() fires, then shellClosed() fires. I need to be able to ignore the shellDeactivated() when the [x] is clicked.
The ShellEvent does not have any pertinent information, it just holds the Shell object, not which Shell control initiated the event.
Is there any way I can trap for the [x] click?

Grumble grumble
Ok, inside shellClosed() I pop-up a message asking the user if they really want to quit. It seems that this is considered a lost focus event (true enough). That is what was firing the shellDeactivated() event.
So the shellClosed() fires, I show a pop-up message, which fires the shellDeactivated() event. And things happen out of order. A simple flag and back to normal :-)

Related

Swing: text box focus lost event vs button clicked event

I have a Form with various textboxes(say around 10) .After the user fills value in each textbox, it is validated on focuslost event for the textbox.
public void focusLost(FocusEvent e)
{
JTextField tf = (JTextField)(e.getSource());
String finalVal = tf.getText();
try
{
validate(finalVal);
}
catch(NmfException ex)
{
JOptionPane.showMessageDialog(parent, message, title,
JOptionPane.ERROR_MESSAGE);//Error Message is passed
/* Error pop up is displayed when validation fails. Message text with an 'Ok' button is displayed and the code waits for ok to be clicked to execute rest of the code*/
tf.setText(defaultVal);//Value is reset to default value
return;
}
}
The form has a 'Add' button which gets the values from the UI(from the textbox) and sends it to the server.Ideally, since the values are validated at each textfield the value sent to the server should be valid inputs.
But my issue is, when an invalid input is given to a textfield(say -5 an invalid input) and 'Add' button is clicked at once.
The focusLost event is triggered and the pop up is obtained,while the code waits for the 'OK' button in pop up to be pressed,the next event of button clicked is also called.So before the defaultVal can be set as textfield value,the Add button operation is done(there is no further validation in add operation) and invalid inputs are sent to the server.
How can ensure that Add operation is called only after the focusLost event operation is done.Please suggest a fix for the issue? What would be a best practice for such a scenario?
Set one Flag which should be check while click on 'Add'.
So if all validation should be true/OK then send to server.
if flag is false/invalid, while click on 'Add' then give user prompt
with error message.
As per your scenario if any one try to add invalid value then
focusLost event makes Flag -> false, and vice-versa.
Likewise need to design architecture of coding.
You could also use a mouse listener on the text fields, and validate in the mouseExited method

JavaFX: Stage close handler

I want to save a file before closing my JavaFX application.
This is how I'm setting up the handler in Main::start:
primaryStage.setOnCloseRequest(event -> {
System.out.println("Stage is closing");
// Save file
});
And the controller calling Stage::close when a button is pressed:
#FXML
public void exitApplication(ActionEvent event) {
((Stage)rootPane.getScene().getWindow()).close();
}
If I close the window clicking the red X on the window border (the normal way) then I get the output message "Stage is closing", which is the desired behavior.
However, when calling Controller::exitApplication the application closes without invoking the handler (there's no output).
How can I make the controller use the handler I've added to primaryStage?
If you have a look at the life-cycle of the Application class:
The JavaFX runtime does the following, in order, whenever an
application is launched:
Constructs an instance of the specified Application class
Calls the init() method
Calls the start(javafx.stage.Stage) method
Waits for the application to finish, which happens when either of the following occur:
the application calls Platform.exit()
the last window has been closed and the implicitExit attribute on Platform is true
Calls the stop() method
This means you can call Platform.exit() on your controller:
#FXML
public void exitApplication(ActionEvent event) {
Platform.exit();
}
as long as you override the stop() method on the main class to save the file.
#Override
public void stop(){
System.out.println("Stage is closing");
// Save file
}
As you can see, by using stop() you don't need to listen to close requests to save the file anymore (though you can do it if you want to prevent window closing).
Suppose you want to ask the user if he want to exit the application without saving the work. If the user choose no, you cannot avoid the application to close within the stop method. In this case you should add an EventFilter to your window for an WINDOW_CLOSE_REQUEST event.
In your start method add this code to detect the event:
(Note that calling Platform.exit(); doesn't fire the WindowEvent.WINDOW_CLOSE_REQUEST event, see below to know how to fire the event manually from a custom button)
// *** Only for Java >= 8 ****
// ==== This code detects when an user want to close the application either with
// ==== the default OS close button or with a custom close button ====
primaryStage.getScene().getWindow().addEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST, this::closeWindowEvent);
Then add your custom logic. In my example i use an Alert popup to ask the user if he/she want to close the application without saving.
private void closeWindowEvent(WindowEvent event) {
System.out.println("Window close request ...");
if(storageModel.dataSetChanged()) { // if the dataset has changed, alert the user with a popup
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.getButtonTypes().remove(ButtonType.OK);
alert.getButtonTypes().add(ButtonType.CANCEL);
alert.getButtonTypes().add(ButtonType.YES);
alert.setTitle("Quit application");
alert.setContentText(String.format("Close without saving?"));
alert.initOwner(primaryStage.getOwner());
Optional<ButtonType> res = alert.showAndWait();
if(res.isPresent()) {
if(res.get().equals(ButtonType.CANCEL))
event.consume();
}
}
}
The event.consume() method prevents the application from closing. Obviously you should add at least a button that permit the user to close the application to avoid the force close application by the user, that in some cases can corrupt data.
Lastly, if you have to fire the event from a custom close button, you can use this :
Window window = Main.getPrimaryStage() // Get the primary stage from your Application class
.getScene()
.getWindow();
window.fireEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSE_REQUEST));
Ahh this is a known bug in JavaFX where the Stage will not close if a modal dialog is present at the time of closing. I will link you to the bug report which I just saw today. I think it is fixed in the latest release.
Here you go:
https://bugs.openjdk.java.net/browse/JDK-8093147?jql=text%20~%20%22javafx%20re-entrant%22
resolved in 8.4 it says. I think this what you are describing.
public Stage getParentStage() {
return (Stage) getFxmlNode().getScene().getWindow();
}
btnCancel.setOnAction(e -> {
getParentStage().close();
});

How to clear libgdx listeners

So in menu i have 3 buttons and all are with the same code. For the topic only important thing is
button2.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
game.setGame();
dispose();
return true;
}
});
Then everything is going fine until:
button3.clearListeners(); // MainMenu.java:174 line in exeption
and then exeption pops out:
Exception in thread "LWJGL Application" java.lang.IllegalStateException: Invalid between begin/end.
at com.badlogic.gdx.utils.DelayedRemovalArray.clear(DelayedRemovalArray.java:125)
at com.badlogic.gdx.scenes.scene2d.Actor.clearListeners(Actor.java:261)
at com.racostyle.avdelux.MainMenu.dispose(MainMenu.java:174)
at com.racostyle.avdelux.MainMenu$3.touchDown(MainMenu.java:123)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:55)
at com.badlogic.gdx.scenes.scene2d.Actor.notify(Actor.java:165)
at com.badlogic.gdx.scenes.scene2d.Actor.fire(Actor.java:136)
at com.badlogic.gdx.scenes.scene2d.Stage.touchDown(Stage.java:277)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:300)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:200)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
EDIT: if i don't remove listeners they are still active and can be clickable, afc with exeptions.
It looks like you're calling MainMenu.dispose within an actor.touchdown listener. MainMenu.dispose clears all listeners on a button. LIBGX won't allow that.
Why? Because LIBGDX is in the process of calling touch events on a lot of actors. You really don't want your game functioning different depending on the order buttons were added to your stage.
So, call clear listeners somewhere else.
Since you're doing this within a dispose method anyway, I'd suggest NOT removing your button listeners at all, as the actor you've registered listeners for should not be used again.
Its not allowed to just call clearListeners() without event;
Will you post your codes in jsfiddle.. i will try to debug it and add some codes..
Thanks ..

How to limit ModifyListener for user interaction only

I have a textbox with attached ModifyListener.
In implemented modifyText(ModifyEvent e) I execute desired functionality.
The problem with that, that this event is triggered on every text change.
I don't want it to trigger if text was altered programmaticly (by setting text via code).
I want it to trigger only when user changes the code (I can't use keylistener because it will be triggered also when user click on arrow buttons and etc, it also won't detect if user copy&paste text)
You could unregister your ModifyListener before calling setText(..) and reregister it afterwards.
How about textBox.addKeyListener(...) and textBox.addMouseListener(...) instead of ModifyListener?
You can try using Focusout listener.... then you will get the value which user has entered only once.
Text text;
text.addListener(SWT.FocusOut, new Listener() {
#Override
public void handleEvent(Event arg0) {
//Your code here.....
}
});

How to stop user from generating more events?

I am developing a java swing desktop application. The dialog form has an ok and cancel button. When the user clicks ok button the application does some processing. How can I stop user from clicking ok again before the event on ok button has finished executing. Also, i dont want the user to able to press the cancel button till ok has finished executed. Any help will be highly appreciated.
Enablement management is an integral part of UI logic. Action helps you doing so:
Action action = new AbstractAction("myAction") {
public void actionPerformed(ActionEvent e) {
setEnabled(false);
doPerform();
setEnabled(true);
}
};
button.setAction(action);
Beware: long running task must not be executed on the EDT, so this is for short-term only, to prevent the second or so click having an effect
Edit
just noticed that you tagged the question with jsr296, then it's even easier: you can tag a method of your presentation model as #Action and bind its enabled property to a property of the model
#Action (enabledProperty == "idle")
public void processOk() {
setIdle(false);
doStuff;
setIdle(true);
}
Plus there is support (much debated, but useable) for Tasks: basically SwingWorker with fine-grained beanified life-cycle support
If you want to disable all the controls, then I'd suggest using a GlassPane. See here for more info: http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html
The easiest way is to disable the button just after clicking it , and when the action performed is complete then re-enable it.
You can disable your button like yourButton.setEnabled(false); during your processing method and enable it again when it finishes.
It seems that what you want is something like a waiting cursor.

Categories