This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

adding listeners

 
Jeff Monroe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48402
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
 
Jeff Monroe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic