Why JDialog doesn't close after JOptionPane showMessageDialog? - java

When my server app is starting, a JDialog opens to indicate to the user that the app is loading. But between the opening of JDialog and its closing, I use JOptionPane.showMessageDialog() to display another message.
The problem is, if I display this new message before closing JDialog then JDialog will never close even if I close JOptionPane dialog. If I remove the JOptionPane dialog then the JDialog closes as usual.
Why opening JOptionPane.showMessageDialog() disable JDialog closing ?
Is use this code to open JDialog:
final JDialog dlg = new JDialog(this, "Veuillez patienter, le serveur démarre...", true);
dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dlg.setSize(300, 75);
dlg.setResizable(false);
dlg.setLocationRelativeTo(this);
Thread t = new Thread(() -> {
dlg.setVisible(true);
});
t.start();
And this code to close it:
dlg.setVisible(false);
And between these lines of code I do this to open message dialog:
JOptionPane.showMessageDialog(this, String.format(I18n.i18n.getString("PopupWifiCreated"), this.SSID, this.password), null, JOptionPane.INFORMATION_MESSAGE);
Anyone has an idea?
Thanks.

In my opinion setting the visibility of any frame to false is not the greatest idea. Of course it depends what are your goals, but I'd still not recommend it. It should be done like e.g.:
dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dlg.close();
or
dlg.dispose();
If it still wont work, try to reverse the order of dialogs - first show messageDialog, and then JDialog.

Related

How call a JFrame from my Application Window SWT Builder

Here is my code in my ApplicationWindow. I have a widgetSelected happening for a bottom called "Welcome" that I want to open a new window with text, which I already have programmed.
//Welcome was clicked
mntmWelcome.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
Welcome2 newWindow = new Welcome2();
newWindow.setVisible(true);
}
});
And the welcome is a JDialog only showing some text and stuff, but when I use this the program crashes and I get
java.lang.IllegalArgumentException: defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, or DISPOSE_ON_CLOSE
and I have no idea where to set this, i tried within the override but the window never opens. I just want it to open and the previous window should still be there behind. How can I solve this?
Try adding this:
newWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Please, refer to: https://chortle.ccsu.edu/java5/Notes/chap56/ch56_9.html
and read this https://docs.oracle.com/javase/7/docs/api/javax/swing/WindowConstants.html.
Documentations are really useful for beginners.

Message display on top of other apps

Please help me in showing popup in front of browser. I have used following code, but it's coming behind browser once I clicked a save button.
JFrame frame;
frame = new JFrame("");
JOptionPane.showMessageDialog(frame, "Instance is already created");
You need to specify the window/frame as the first argument to show message dialog in front of that frame. So instead of passing null:
JOptionPane.showMessageDialog(null,
"Instance is already created");
pass the parent Frame

How do i display a confirmation message when the user clicks on the red cross? Java GUI

I'm working on Java and I'm trying to display a confirmation message to the user when he wants to exit but I didn't know where I have to put it exactly. may you help me?
You can stop frame from closing by default by using WindowConstants.DO_NOTHING_ON_CLOSE as peram to below API
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
/* (non-Javadoc)
* #see java.awt.event.WindowAdapter#windowClosing(java.awt.event.WindowEvent)
*/
#Override
public void windowClosing(WindowEvent e) {
//Use JOptionPane. If everything goes fine
//then do frame.dispose();
}
});
If your exit is being done by pressing the x button on the window then you need to handle the windows events.
To get the confirmation message you need to pop up a JOptionPane.
There is a discussion of a number of ways to handle the window closing here:
How can a Swing WindowListener veto JFrame closing
Documentation on JOptionPane is here:
http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html
Add a WindowListner to your JFrame and override windowClosing method and do a pop-up with JOptionPane warning user.

Display alert and then jump to another form in j2me

I am working on J2ME application. I want to show alert in Form and display another Form from another class. I have tried the following method to show alert.
public void showMsg()
{
Alert success = new Alert("Data Not found.");
//success.setImage(img2);
success.addCommand(new Command("Ok", Command.OK, 0));
success.addCommand(new Command("Cancel", Command.CANCEL, 0));
success.setCommandListener(this);
success.setTimeout(Alert.FOREVER);
Display.getDisplay(parent).setCurrent(success, chapterForm);
}
After showing the alert I am jumping to another form as:
Display.getDisplay(parent).setCurrent(welcomeForm);
When I run this it don't show the alert but jump to the welComeForm. So, how can I show alert and then jump to another form.
The Alert won't advance automatically to chapterForm because you have replaced the default listener on the Alert with this. Use the commandAction() event in the CommandListener interface to get the OK or Cancel from the Alert. Then you can use Display.setCurrent(Displayable d) to show the Form you want to display.
Display.getDisplay(parent).setCurrent(welcomeForm) is most likely the reason why it don't show the alert but jump to the welComeForm. To be precise it (device) may show alert for a moment, but as soon as you invoke that setCurrent(welcomeForm), it gets momentarily overwritten by welcomeForm.
If you want welcomeForm to be dissplayed by command from alert, just
wipe out the code setCurrent(welcomeForm) from where it is now
insert that wiped-out code into this.commandAction method (this is command listener you use in your code exerpt)
A nifty solution is to start a new thread after setting the current display to the Alert, and in this new thread you can do a Thread.sleep(2000); in order to wait, and after that you display the new form.

JOptionPane.showMessageDialog Query?

This is the Scenario.
I have Code which intiates a Alram when an error is encountered.
AudioAlarm t = new AudioAlarm(song);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Awake");
t.start();
setRunnung(true);
JOptionPane.showMessageDialog(null, "Alarm ...", "Alarm", JOptionPane.OK_OPTION);
AudioAlarm.setLoop(false);
System.out.println("Alarm Acknowledged ...");
I would like to re-design this logic in this manner,
If the Alarm is unacknowledged by the user over a period of time say 2 min, it turn off and message msg dialog should disappear.
How can I Obtain this?
I am able to Stop the Alram, but unable to dispose the dialog without the user pressing "OK"
To do what you want, you should:
Create a JOptionPane instance using one of its constructors
Call createDialog on this option pane to get a dialog containing this option pane
Use a javax.swing.Timer instance in order to fire an action event after 2 minutes
add an action listener to this timer which would close the dialog containing the option pane
show the dialog containing the option pane.
I do not know if what you are after can be done, but can't you instead replicate the JOptionPane as a JFrame and dispose of that one? You can find how to close a JFrame on this Previous SO Post:
If you want the GUI to behave as if
you clicked the "X" then you need to
dispatch a windowClosing Event to the
Window. The "ExitAction" from Closing
An Application allows you to add this
functionality to a menu item or any
component that uses Actions easily.

Categories