Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"Global Fields" used in listeners

 
Lance Finney
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a gui that uses a couple ActionListeners to respond to button clicks. Within those listeners, I use values entered in fields on the JFrame. I'm currently creating the listeners as inner classes and making the text fields private class variables so the listeners have access to them. I'm concerned about this approach because I'd rather have the text fields local to the methods that create them. This will be a problem if I later decide to pull those Inner Classes out to their own files. Basically, I'm concerned that I'm not following good MVC principles by coupling the listeners to the text fields. Is there a way to do this without using a bunch of getParent() calls on the event?
Here's a simplified version of my code:

Is there a way to use a value from bookText in the listener without doing this?
[This message has been edited by Lance Finney (edited October 18, 2001).]
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option would be to make your buttons ActionListener anonymous and just execute a method within your main class.
 
Lance Finney
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought about using anonymous inner classes, but the code I have is much more complex than what I showed in the simplified code. Because of that, I'd rather not have such complicated code buried in the anonymous class.
Another option would be to have Prototype implement the listeners directly. Maybe I'll just have to do that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic