File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[SOLVED] Disposing JFrame from a different class

 
Lance Gray
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...also moving this to an appropriate forum
 
Lance Gray
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic