Closing JOptionPane.ShowInternalOptionDialog programmatically - java

My current design is like this:
I have an outer frame that displays main app. When user clicks a button on the main app, a pop-up Window should be launched. I am using JOptionPane.showInternalOptionDialog and passing button objects as options. When user clicks one of these button, it should run certain commands and then close the window. I was not able to close the Frame that shows the pop-up.
I found a similar question in this forum, that suggests the following workaround.
Closing a dialog created by JOptionPane.showOptionDialog()
But the above workaround closes my complete gui. I just want to close the JOptionPane popup.
Thanks in advance.

try
JOptionPane.getRootFrame().dispose();
in a event

A couple of solutions:
Create a JOptionPane directly instead of using the showX() methods. This will allow you to have a reference to the dialog that you can pass to your button to allow it to call the dispose method().
Create your own dialog instead of using JOptionPane. This would be my preferred option, seeing that you are starting to get away from a simple dialog.

I had the same problem. I solve it using a thread that close my JOptionPane after X milliseconds.
import javax.swing.JOptionPane;
public class JOptionPaneExample {
public static void main(String[] args) {
final JOptionPane pane =new JOptionPane();
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
pane.getRootFrame().dispose();
}
});
t1.start();
pane.showMessageDialog(null, "I will close after 3000 milli's", "Programmatic Auto-close JOptionPane", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}

By default, clicking a button in a JOptionPane will close the dialog. If yours doesn't, it's because you are using actual components rather than strings or other objects, and it is calling your buttons' custom event handlers instead of its own.
I would recommend you take a different approach, using strings instead of buttons. Use an array of buttons as your input values, and when the call to showInternalOptionDialog returns, you can check the int index to find out what was pressed, and then switch on it in your code. This way, you don't have to touch the JOptionPane at all and it will close by itself.

Related

Saving last window properties/content in javafx

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());

How to close a called class using JFrame without closing the class that called it?

I seem to have a fairly unique problem, and I searched for a while for an answer on here without finding one. I have a class that has a simple JFrame with two buttons. Each button calls the Main method of a different class, as such:
checkRuling = new JButton("Check Your Deck's Rulings");
checkRuling.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
ReadHtmlCardDatabase.main(null);
}
});
One calls a class that takes a series of inputs into a text field and creates a formatted html document from the inputs, and the other loads the html document into a JEditorPane. My problem is that when I close one of the JFrames for the subclasses (either the input or html loader one), it exits my program completely, and I want to keep the main class (with the two buttons) open. I've tried using:
close = new JButton("CLOSE");
close.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
System.exit(1);
}
});
On a button in the subclasses, to no avail. When the button is clicked it simply exits everything. I've also tried using:
JFrame.DISPOSE_ON_EXIT
For the subclasses, but this causes the JFrames to go away without the subclasses actually closing, so the first one that saves the html document never actually saves it, and the second subclass that opens that same html document won't work, because it wasn't saved. Any help would be appreciated, because I can't figure out how to do this.
As Fast Snail says in the comments, you shouldn't be calling a main method. Instantiate a class that does each functonality. Set the frame to visible using setVisible(true) when you start using it, then setVisible(false) when you're done. So, in the action listener, just change the visibility.
Then, assuming you don't have anything too wild going on, the frame you just set to invisible should go out of scope and get freed so that memory isn't chewed up. You just instantiate a new copy of the ReadHtmlCardDatabase class each time you need one. Or you could have one static copy that you set visible/invisible as needed.
one of the JFrames
You should use only one JFrame in Your GUI. For other windows You can use for example JDialog or JWindow.
This should help, if not You can always use frame.setVisible(false) instead of dispose on close, but it' s not very neat.
Thanks to someone who posted a comment and then deleted it, I've figured out my own problem. I just had to replace my main call with this:
setDeck = new JButton("Set Deck");
setDeck.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
WriteHtmlCardDatabase w = new WriteHtmlCardDatabase();
w.main(null);
}
});
Thank you!

How can I close the main GUI in Java SE?

Using swing, I want to close my mainGUI with "setVisible(false)", how can I close it when I opened it like this:
public void run() {
new mainGUI().setVisible(true);
}
I want to close the GUI from a method like this:
public void close(){
//Close GUI here
}
What call is missing from my code to let the GUI close itself?
It depends the place you wanna do that:
In the same class
// Close the window
this.detach();
or
// Hide the window
this.setVisible(false);
In other class you need to send the window like a param and use the same methods.
// Close the window
window.detach();
or
// Hide the window
window.setVisible(false);
I want to close the GUI from a method like this:
Presumably you invoke this method when the user clicks on a button or menu item. So the best way is to create an Action that you add to the component. Then the Action will be executed when the component is clicked.
For example you can use the Exit Action found in Closing An Application. The ExitAction will find the current window with focus and then close it.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) or you could use focus listener
Go through this in java docs this might help you

Closing on a single window closes all the frames in java

I would like to make possible the navigation of frames in java.Whenever i close a frame the remaining frames which are also opened get closed;and the entire program stops.
Please help...
You probably used
//this will terminate or exit your application
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Maybe you want to use this instead,
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
for your reference go to this link
If you want to close only that one frame, you should do something like this: setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
If you want to close all frames whenever a single frame closes you can do the following:
You could use a window listener and call System.exit(0); when the JFrame closes, or try setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); on each JFrame. That way your program would close all frames and end.
If you need to perform some tasks before application quits, you should probably use the window listener.
You can also do that graphicaly. right click on your frame and select properties and from there you can change that like the below picture.
If you were using swing palette. In frame properties choose default close operation as (Dispose). Follow as per the image given in this solution.
My problem was that I used a listener found on the basic tutorials :
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(l);
I know it's dumb. I didn't see it, but some people might have done the same thing, so I'll just leave this here ;)

Use invokeLater to show dialogs one by one

In 2 different action listeners, a dialog will be shown when some conditions are met.
If both of the action listeners need to show dialog, 2 dialogs will be shown at the same time. But I want them to be shown one by one.
Simplified code:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(getTopLevelAncestor(), "dialog 1");
}
});
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(getTopLevelAncestor(), "dialog 2");
}
});
Those 2 "SwingUtilities.invokeLater" invocations are in different classes.
Make a class that keeps track about that; this class would contain a queue of dialogs to display; whenever a dialog is closed, the first one of the queue is shown and removed from the queue. When another class needs to show a dialog, it's immediately shown if the queue is empty, or inserted into the queue else.
This is related to the modality of dialogs. There is quite useful article about this topic http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/modality/. Dialogs have different modality types with different priorities. You can solve your problem by creating the second dialog with lower priority:
JOptionPane pane = new JOptionPane("dialog 2", JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog("Message");
dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
dialog.setVisible(true);
Hopefully this helps.
You need invokeAndWait(), this method waits until the Runnable has been finished.
Or in your situation, when the first dialog has been closed.

Categories

Resources