Solved

Using JComponent.firePropertyChange() with a Swing component

Posted on 1998-07-28
6
2,387 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Should localization be done inside spring controller 5 36
hibernate jars 4 57
JavaFX TableView not displaying correctly 3 103
How to log java errors in tomcat 8 24
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

763 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