my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes adding listeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "adding listeners" Watch "adding listeners" New topic
Author

adding listeners

Jeff Monroe
Greenhorn

Joined: Apr 25, 2006
Posts: 3
Hi this is kind of a generic implementation question...

If I have a panel that contains many text fields and any time one of these fields changes I'd like to [if appropriate] enable/disable a menu item or tool bar button based on the change what is the best practice in doing so? What is the easiest/best approach to listen for changes across a whole panel?

For Example: Let's say I have a "save" menu button I only want to enable if something is added or changed to the current visible panel and all components (i.e., menu bar, tool bar, main panel) are separately instantiated components added to the FrameView. How would a menu item listen for a change in the main panel?

Thanks for any advice.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
Welcome to JavaRanch
Please remind yourself of the naming policy and confirm whether you are displaying a real name.

There are all sorts of Listeners you can add to Components. You can have things like KeyListeners or DocumentListeners which listen for changes in text, or ComponentListeners and ContainerListeners which listen for hiding, showing, adding, removing things, etc. You can tell what sort of Listeners a particular type of Component supports by going to the API documentation and looking for addXYZListener methods in each class.

You can set up a boolean somewhere with a name like "needsSaving;" for any of those changes you can set it to true, probably with a setNeedsSaving() method. In that method you find the "save" button and invoke its setActivated method. In the save button's ActionListener you can call its setActivated method again.

You can also add a WindowListener to your top-level frame, so when it closes and needsSaving is true, you can show a "Do you wish to save changes" dialogue.
Jeff Monroe
Greenhorn

Joined: Apr 25, 2006
Posts: 3
Okay... I'm being dumb. I appreciate your response and totally get the concept. But...

for reference - I'm trying to implement an application using the java AppFramework and I have a SingleFrameApplication with a FrameView that adds custom [each self-contained] component classes for menus, toolbars, content, and status bar to the frame. A custom JTabbedPane gets added to the main content area and custom JPanels get added as each tab. How does an action listener get added to the fields on the custom JPanel (like a text field) and then the listener get added to the file menu or tool bar item? Do I have to add and action listener to every field on the JPanel and listen for all of them on the menu item and/or tool bar button I want to disable based on any change? I'm thinking that a custom ActionListener is order here, but fundamentally not sure since the custom ActionListener would have to "know" about both the menu and toolbar.

Maybe more simply put how do you register a listener on a component that lives in one object [class] and listen for it on another component that lives on a completely different object [class] where neither is necessarily aware of the other except for the "listener" relationship?

As you can see by my question I'm a bit confused. Anyone have any simplistic approach to this for understanding sake?

Thanks kindly.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
some ideas here

http://www.coderanch.com/t/346666/Swing-AWT-SWT-JFace/java/Dirty-Forms#1521264
Jeff Monroe
Greenhorn

Joined: Apr 25, 2006
Posts: 3
Michael, thanks for that thread link... Here are a couple of excellent articles regarding the topic of my question;

Maybe more simply put how do you register a listener on a component that lives in one object [class] and listen for it on another component that lives on a completely different object [class] where neither is necessarily aware of the other except for the "listener" relationship?


http://www.developer.com/java/ent/article.php/3685486
http://www.developer.com/design/article.php/10925_3678856_1

The way these articles were written makes this very easy to understand.
 
Don't get me started about those stupid light bulbs.
 
subject: adding listeners