How can I close the main GUI in Java SE? - java

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

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 single class that contains frame in java

i wan to close the particular classname.java window alone.
I am developing a GUI using netbeans.
I created a main class and defined a button , when clicked will load
new classname().setVisible(true);
this classname.java contains a frame with components to get the input
now when i use System.exit(); in classname.java all the windows are closed
i wan to close the particular classname.java window alone.
how can i do so ?
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0); // TODO add your handling code here:
}
USE DISPOSE_ON_CLOSE
JFrame f = JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)`
If you just want to hide it use
setVisible(false);
Or to dispose use
dispose();
Make sure you keep a reference to the classname instance you created, and call either setVisible(false) or dispose() on it.
When you do that,it will close all the windows opened, it means that the app will be ended.
I think you should use this method: windowClosing(). You can get more information about this, here:link
I hope that is helpful to you.
Regards, Gil

Java add ActionListener to special button

Is there a way to add/override the action event of THIS button ? I couldn't find anywhere how to acces this button and override it's action. In my case I need to do this because I need to save the resources before I exit my window, and if I press the x button, It will exit automatically.
On the JFrame:
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
Then add a WindowsListener and in the windowClosing(WindowEvent event) methods do your job and then call dispose()
I'd use a shut-down hook instead that way it'll save even if you close the app via another method.
Looky here
You can try using a WindowListener interface. If I remember correctly, it should allow you to do things when something on the frame is clicked on(like exiting).
See http://docs.oracle.com/javase/6/docs/api/java/awt/event/WindowListener.html

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

Closing JOptionPane.ShowInternalOptionDialog programmatically

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.

Categories

Resources