wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes [SOLVED] Disposing JFrame from a different class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "[SOLVED] Disposing JFrame from a different class" Watch "[SOLVED] Disposing JFrame from a different class" New topic
Author

[SOLVED] Disposing JFrame from a different class

Lance Gray
Greenhorn

Joined: Dec 06, 2011
Posts: 20
I have a JFrame called MainFrame that contains a JPanel called choicesPanel. Inside the choicesPanel is a bunch of CustomButton objects (which is a class that inherits from JButton). What I want to do is to dispose MainFrame when I click the CustomButton object inside choicesPanel?

Here's what I've done so far.




Thanks in advance.

EDIT: I'm using NetBeans IDE, btw.
EDIT 2: Solved using Mohamed Sanaulla's hint. For those that wants to know, what I did is make CustomButton receive a JFrame object. Then when MainFrame creates the CustomButton object, I passed this.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Your CustomButton should have access to the MainFrame instance inorder for you to invoke dispose on it. So you either pass a reference to MainFrame instance when you are creating instances of CustomButton.

Also check out the loop where in you are setting the layout of the choicesPanel. You seem to be updating the layout in each loop. I think you should be setting the layout before the loop starts.


Mohamed Sanaulla | My Blog
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

...also moving this to an appropriate forum
Lance Gray
Greenhorn

Joined: Dec 06, 2011
Posts: 20
Mohamed Sanaulla wrote:Your CustomButton should have access to the MainFrame instance inorder for you to invoke dispose on it. So you either pass a reference to MainFrame instance when you are creating instances of CustomButton.

How do I do that? Can you post a sample code?

Mohamed Sanaulla wrote:Also check out the loop where in you are setting the layout of the choicesPanel. You seem to be updating the layout in each loop. I think you should be setting the layout before the loop starts.

It was supposed to be the same number as maxButton but I skipped fixing it since I got stuck in the disposing part. Thanks for the reminder.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

The way I would think is: You could do this:


I see some issue here: You would have maxButton number of JButtons and each of them would be responsible for disposing the frame? Why not have one button responsible for disposing the frame?
Lance Gray
Greenhorn

Joined: Dec 06, 2011
Posts: 20
Mohamed Sanaulla wrote:The way I would think is: You could do this:

Thank you for the answer. I have solved it using the answer on your previous post.

Mohamed Sanaulla wrote:I see some issue here: You would have maxButton number of JButtons and each of them would be responsible for disposing the frame? Why not have one button responsible for disposing the frame?

Each CustomButton opens a new frame depending on the value of i, and when one of them are clicked, the new frame is displayed and the old frame (MainFrame) is disposed of.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

There are a few things:
You can leave the subject as it is or may be if you feel its resolved there's button which you can use for suggesting that its resolved.

And yeah your: "initComponents()" method suggested that you were using Netbeans GUI builder

And welcome to JavaRanch!
 
Don't get me started about those stupid light bulbs.
 
subject: [SOLVED] Disposing JFrame from a different class
 
Similar Threads
Pressing ENTER when Button is selected doesnt fire buttonActionPerformed...
how to do big table pagination reading from database with jtable?
SWT n Jface
JTable problem
Focusing frames..