Solved

Using JComponent.firePropertyChange() with a Swing component

Posted on 1998-07-28
6
2,404 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 

Author Comment

by:mwalker
ID: 1228848
Edited text of question
0
 

Author Comment

by:mwalker
ID: 1228849
Edited text of question
0
 
LVL 5

Accepted Solution

by:
msmolyak earned 150 total points
ID: 1228850
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:mwalker
ID: 1228851
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
ID: 1228852
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
ID: 1228853
Yes.  Thank you.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
login form jsp example 2 71
learn programming 8 94
activeMQ Queue Messages Enqueued and Messages Dequeued 1 116
Selenium findElement(By.classname  identifier 15 34
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.
Suggested Courses

734 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