Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using JComponent.firePropertyChange() with a Swing component

Posted on 1998-07-28
6
Medium Priority
?
2,475 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 450 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
Independent Software Vendors: 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!

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…
Suggested Courses

715 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