wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Closing a JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Closing a JFrame" Watch "Closing a JFrame" New topic

Closing a JFrame

Ben Wong

Joined: Mar 27, 2006
Posts: 19
I have one main JFrame for my program which creates another JFrame, like a JOptionPane. However, I chose not to use a JOptionPane because the new Jframe contains a form in which the user enters some data.

My question is: how do I close this second JFrame when the user clicks the "OK" button? Is it possible without using a JOptionPane?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299

On your second JFrame OK button's event handler, just call dispose().

GenRocket - Experts at Building Test Data
Ben Wong

Joined: Mar 27, 2006
Posts: 19
Ben Wong

Joined: Mar 27, 2006
Posts: 19
The problem is that my first class instantiates the JFrame for the second class. The second class extends JPanel and therefore cannot see the dispose() method.

The solution I have so far is to have the second class nested in the first.
It is sorta covered in the JavaRanch Style Guide.
subject: Closing a JFrame
Similar Threads
JFrame confusion
How can I customised the Ok-Cancel Button in JOptionPane
partial display of JOptionPane
Cannot take populate JTextfield
Calling JDialog from JDialog - Urgent please...