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
Solved

Using JComponent.firePropertyChange() with a Swing component

Posted on 1998-07-28
6
2,373 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
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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
servlet requestdispatcher include and forward differences 1 39
Way to decrease size of apk file 9 86
Android development question 2 54
Java: anonymous class 4 29
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

860 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