Close One Jframe Without Closing Another

Close one JFrame without closing another?

If you do not want your application to terminate when a JFrame is closed, use

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)

instead of

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

From the documentation:

  • DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.
  • HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects.
  • DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects.
  • EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications.

This was my answer before the question was clarified, might still be useful:

You can use setVisible(false) on your JFrame if you want to display the same frame again.
Otherwise call dispose() to remove all of the native screen resources.

How do you close a JFrame window without closing another one?

You duplicated the JFrame, created a JFrame field f inside the JFrame.
Do not use static components like the button.

public class Frame1 extends JFrame implements ActionListener {

private final JButton login = new JButton("Login");

Frame1() {
setTitle("Login");
setSize(1000, 750);
setLocation(750, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);

login.setBounds(250, 350, 150, 30);

add(login);
login.addActionListener(this);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == login) {
Frame2.frame2windown();
}
}

public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Frame1 login1 = new Frame1();
}
}
}

Use the swing/awt event queue (invokeLater) as on this thread window events are handled and dispatched further.

And Frame2:

public class Frame2 extends JFrame implements ActionListener {

private JButton logout = new JButton("Logout");
private JLabel jb1 = new JLabel("Hello And Welcome");

Frame2() {
setTitle("Logout");
setSize(1000, 750);
setLocation(750, 250);
setLayout(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

jb1.setBounds(250, 150, 350, 30);
logout.setBounds(250, 350, 150, 30);
add(logout);
add(jb1);
logout.addActionListener(this);
setVisible(true);
}

public void actionPerformed(ActionEvent a) {
if (a.getSource() == logout) {
setVisible(false); // <--- ALL
}
}

public static void frame2windown() {
Frame2 f2 = new Frame2();
}
}

JFrame.setVisible does it all. Especially setVisible(true) should maybe even done after the constructor is called, so it always is last.

Another remark, dive into layout managers fast. Absolute layouts (null) are a PITA.

How to close a jframe without closing the main program?

You can simply hide the JFrame.

this.hide();

Edit: For the above: Youssef G's answer is better.

For the 2nd part of the question. Create your tree class and pass the object around in your program so it is the same tree object. Don't create a new one.

For example:

Class A {
B b; //B object inside class A
Tree t; //Tree object inside class A
}
Class B {
Tree t; //Tree object inside class B
}

Now you have a tree object in both classes. You can create a new tree which is class b's tree while code is running in Class A. Then say this.t = b.t;

Hope this helps.

how to close only one awt Frame without closing other?

System.exit(); causes the Java VM to terminate completely.

So you need to use JFrame.dispose(); that causes the JFrame window to be destroyed and cleaned up by the operating system.

Frame f2 = new Frame();
f2.setVisible(true);
f2.setLayout(null);
f2.setSize(500,500);
f2.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent w)
{
f2.dispose();
}
});

Explanation from this post

How to close JFrame without closing another

This...

LoginForm exit = new LoginForm();
exit.dispatchEvent(new WindowEvent(exit, WindowEvent.WINDOW_CLOSING));

makes no sense at all, the instance of LoginForm you've created here has nothing to do with the one which was created eailier, in fact, Loading has no responsibility for managing it, it's beyong it's scope.

A simple solution would be to make use of an "observer pattern", so that when Loading has actually completed, it can inform any intertested parties and they have can take care of what should be done next.

For example...

public class Loading extends javax.swing.JDialog {

public interface Observer {
public void loadingDidComplete();
}

private Observer observer;

public Loading(java.awt.Frame parent, Observer observer) {
super(parent, true);
this.observer = observer;
initComponents();
this.setBackground(new java.awt.Color(0, 0, 0, 0));
Timer timer = new Timer(10000, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
observer.loadingDidComplete();
}
});
timer.setRepeats(false);
timer.start();

}

And then in your LoginForm you could so something like...

private void LoginMouseClicked(java.awt.event.MouseEvent evt) {
Loading loading = new Loading(this, new Loading.Observer() {
@Override
public void loadingDidComplete() {
dispose();
JFrame frame = new MainMenu();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
loading.setLocationRelativeTo(this);
loading.setVisible(true);
}

Oh, and you really, really need to look at How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listener, again, stop using the form editor, it's not doing your any favours

However, oddly enough, since you're using a modal dialog, you could simply do...

private void LoginMouseClicked(java.awt.event.MouseEvent evt) {
Loading loading = new Loading(this);
loading.setLocationRelativeTo(this);
loading.setVisible(true);

dispose();
JFrame frame = new MainMenu();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

and get the same effect

Java: How do I close a JFrame while opening another one?

The method JFrame.setVisible can be used to hide or display the JFrame based on the arguments, while JFrame.dispose will actually "destroy" the frame, by closing it and freeing up resources that it used. Here, you would call setVisible(false) on the picture frame if you intend to reopen it, or call dispose() on the picture frame if you will not be opening it again, so your program can free some memory. Then you would call setVisible(true) on the main frame to make it visible.

Is there any way to Close the JFrame without exiting the whole application using Cancel jButton?

Use anything for setDefaultCloseOperation excepting EXIT_ON_CLOSE.

Then, for the cancel button, just dispose() the window. If the application has another running thread, it won't exit. You can also hide a window by calling setVisible(false).



Related Topics



Leave a reply



Submit