Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Attempting to call dispose() Method on Jframe from an ActionEvent  RSS feed

 
Matthew Matthew
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
My research culled the following code from SUN forums on calling a button that will dispose() of the frame that is is currently in. When I click on it though, It deletes not the frame with the button where the action event occurs but it's parent (the main) Jframe. Here is the code:


Doing event handling this way (which I just pulled from SUN forums), it still feels kludgey to me. I can't call f.dispose() in it's own event without declaring f final. I can't declare f final because I am populating it with dynamic data. I wrote my own event handler in a seperate class and it just ignored dispose.
Any ideas?
While writing this, it occured to me that this whole thing is occuring within an event handler for the parent (User clicks a button, this frame pops up) which may tell dispose what to actually dispose. I will try to move it.

Yeah, I just pulled all of that code out of the event handler and it still does the same thing
 
Matthew Matthew
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah . . . I see that this was the last post within the hour that still has no replies to it . . . at any rate, here is the solution - first, don't pull kludgy code from smug jerks on forums.
second, add your own event handler that you can pass whatever needs to be disposed:

Define button seperately, with its own event handler:

define it's event handler"
 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Matthew wrote:Yeah . . . I see that this was the last post within the hour that still has no replies to it . . .

http://faq.javaranch.com/java/PatienceIsAVirtue

Matthew Matthew wrote:

For the record, p doesn't even have a dispose() method.

I can't declare f final because I am populating it with dynamic data.

Ehm, that's not what final means.

Final means that you can't give variable a new value; once assigned, it will keep that value until the variable goes out of scope. The following is perfectly fine:
What you can't do is the following:



Other than the final variable, this is about the best solution.

In fact, the anonymous class you had in your first post would have had its own reference to the frame as well just like your ActionListener. You just couldn't see it from code since it's an implicit field.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!