Ok I have two frames.. one frame calls the other frame so it's like a dialog box... the second frame pops out, and yeah i want to close this second frame with a button.. how do i do it?? I was also wondering let's just say that I have two buttons in my second frame a YES and a NO... when the YES or NO gets clicked, the second frame closes.. how will the first frame will know which of the two button closed the second frame

Dani AI

Generated

A common pattern is: the child window signals a result to the parent, then disposes itself. , , and already noted that dialogs simplify this, and showed hiding the window — both are valid. If you insist on a separate JFrame, prefer an explicit callback (listener) or a small result object instead of letting the child directly poke at the parent's UI.

Here is a simple, safe listener pattern (keeps responsibilities clear and avoids tight coupling):

public interface ResponseListener {
    void onResponse(String response);
}

public class ChildFrame extends JFrame {
    private final ResponseListener listener;
    public ChildFrame(ResponseListener listener) {
        this.listener = listener;
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        // add YES/NO buttons...
        yesButton.addActionListener(e -> finish("YES"));
        noButton.addActionListener(e -> finish("NO"));
    }
    private void finish(String r) {
        if (listener != null) listener.onResponse(r);
        dispose();
    }
}

Parent implements the interface and updates its UI on the EDT:

public class ParentFrame extends JFrame implements ResponseListener {
    void openChild() { new ChildFrame(this).setVisible(true); }
    public void onResponse(String r) {
        SwingUtilities.invokeLater(() -> myLabel.setText(r));
    }
}

Practical notes: use DISPOSE_ON_CLOSE so resources are freed; prefer passing immutable result objects for complex data; avoid direct component access across windows; ensure UI work runs on the Event Dispatch Thread; and consider a modal JDialog if you want the parent blocked until a choice is made (it often simplifies flow). These patterns keep code maintainable and avoid subtle bugs or memory leaks.

Recommended Answers

All 9 Replies

Instead of a second frame use a dialog box. Read in this JOptionPane the static methods that begin with show...(...) .

Oh come on man.. i want a FRAME.. a window.. don't limit my creativity :(

Oh come on man.. i want a FRAME.. a window.. don't limit my creativity :(

You do know that you can handle a Dialog (or JDialog) in exactly the same way you do a JFrame, except that you do not have, per default, the min max buttons on the frame, right?

You don't have to use JOptionPane. Suggesting the use of, or using, a (J)Dialog does not "limit your creativity" in anyway, and it makes a few things in your life/coding much easier.

Edit: And a Dialog is a Window.

To close the second frame with the button you place the button where you want it, then you use actionperformed on that button. The code is:

/* jFrame1.pack();
jFrame1.setVisible(false); */

When you set the frame visible you have used setVisible with true.

I hope this helped? Because i am new of trying to explain for others.
/Me

ok this time the second frame pops up which was called by the first frame... What i want to do is to get some information to be passed into the first frame when the second frame closes. how do i do that?

ok this time the second frame pops up which was called by the first frame... What i want to do is to get some information to be passed into the first frame when the second frame closes. how do i do that?

Hi!
Well, you should probably put code under the button that are the close button (action performed). So when you close frame2 - this will happen in frame1 -, for an example in your textarea or what you use, in the place you want to show the information.
Or you can make a method with what you want to happen in frame1 and then you call that method from the close button actionperformed.

Good luck!

Of course, this is exactly what a Dialog is for...

But you don't want to limit his creativity, do you? ;-)

Of course not! :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.