Solved

Using JComponent.firePropertyChange() with a Swing component

Posted on 1998-07-28
6
2,325 Views
Last Modified: 2006-11-17
Can someone (in detail) how to set up objects to use the JComponent.firePropertyChange() method when and "action" is performed? Specifically, I have a object (extending from JPanel) that contains a JComboBox. I'd like to have other objects "listen" to this JComboBox to change and automatically do something with the new value.

Source code of an example would be greatly appreciated.
0
Comment
Question by:mwalker
  • 4
  • 2
6 Comments
 

Author Comment

by:mwalker
Comment Utility
Edited text of question
0
 

Author Comment

by:mwalker
Comment Utility
Edited text of question
0
 
LVL 5

Accepted Solution

by:
msmolyak earned 150 total points
Comment Utility
You should define listeners (the classes interested in property change event) by implementing java.beans.PropertyChangeListener interface (which has the propertyChanged() method).

Next step is to register the listener class with the component whose property change events you want to intercept by using JComponent.addPropertyListener() method.

The last step is notifying the registered listeners about the property change. For instance, in the method setCoffeeStrength() which changes the coffeeStrength property value you call the method firePropertyChange("coffeeStrength", oldStrength, newStrength).  This in turn calls the propertyChanged() method of all the registered listeners passing the property name and the old and new values. The listeners decides what to do about this change (depending on how you implement propertyChanged() method).

firePropertyChanged() method is overloaded to cover all the possible property types.

Hope that helps.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:mwalker
Comment Utility
This is good, but, I'm not clear on your example.  When does setCoffeeStrength() get called?  In my case, I have a ActionListener defined on the JComboBox. Would I call the method firePropertyChange("coffeeStrength", oldStrength, newStrength) here?
0
 
LVL 5

Expert Comment

by:msmolyak
Comment Utility
In my example the property change was initiated by someone calling setCoffeeStrength().  In your case you want to link the notification to the value selected in the combo box. Rather than using property change mechanism you can use the item listeners. It is even simpler.

Your listener will implement the ItemListener interface. Then you add the listener(s) to the combo box using addItemListener() method. When the selection changes the listener is AUTOMATICALLY notified (that  the method fireItemStateChanged() method gets called for you, you do not have to call it. This method will tell the listener what the event source is as well as the old and new selection.

Is that what you want?
0
 

Author Comment

by:mwalker
Comment Utility
Yes.  Thank you.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now