Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-10-2002, 10:19 PM   #1 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
listening...

I want to monitor the state of some of my objects' fields' for change, and when it does change I want to do something, so instead of constantly checking if it had changed with if statements, etc, I thought it would be a good idea to add a propertyChangeListener() (or any other appropriate listener) to it... now, this object isn't related to graphics in anyway, its just an object so i don't know how to do it..

any ideas? hints on where to start?
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 10-11-2002, 05:08 PM   #2 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
figured this one out too.
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 10-12-2002, 09:56 AM   #3 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
technobard is on a distinguished road
Inquiring minds want to know, what was the solution? I didn't think it was possible without a gui.
technobard is offline   Reply With Quote
Old 10-12-2002, 03:02 PM   #4 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
well, this was my test code:

Code:
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyChangeEvent;
import javax.swing.event.SwingPropertyChangeSupport;

class test
{

	PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
	int x = 50;
	long y = 0;


	public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener){
		if(listener == null){
			return;
		}
		if (changeSupport == null){
		    changeSupport = new PropertyChangeSupport(this);
		}
		changeSupport.addPropertyChangeListener(propertyName, listener);
    }

    public void firePropertyChange(String name, int oldVal, int newVal){
        if ((changeSupport != null) && (oldVal != newVal)) {
            changeSupport.firePropertyChange(name, new Integer(oldVal), new Integer(newVal));
		}
	}

	public void setX(int x){
		if(this.x == x){
			System.out.println("not changed.");
			return;
		}
		int oldx = this.x;
		this.x = x;
		changeSupport.firePropertyChange("x", oldx, x);
		System.out.println(x);
	}

	public static void main(String[] s){
		test t = new test();
		t.setX(50);
	}

	public test(){
		this.addPropertyChangeListener("x", new PropertyChangeListener(){ public void propertyChange(PropertyChangeEvent pce){ System.out.println("i was changed"); }});
	}

}
i basically looked through the source of JComponent, Container, etc, for how to do it in the java.src zip file

but upon realizing how its done, im not so sure if i need it anymore, because it really is only uesd to notify other objects of the its' states' changing, not itself, or something, and that is what that test prog is doing, but it may be useful.
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 10-13-2002, 09:20 AM   #5 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
technobard is on a distinguished road
Okay. So you create a manager object (changeSupport) and you register objects that have members that want to be monitored with the manager. Then you add/create listeners (PropertyChangeListener) to listen for events around the registered object member.

As part of the listener creation, you specify an action that occurs when a property changes by specifying the propertyChange(PropertyChangeEvent pce) method. Presumably you could modify this to include a method call on the listening object that would update totals, for example, that included the changed object.

I could see this being useful. I also understand why it is normally tied to a GUI since it would make sense to refresh a display after updating values.

Thanks for sharing the code.
technobard is offline   Reply With Quote
Old 10-13-2002, 04:02 PM   #6 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
yea, i decided it would only be useful for 3+ objects, A, B, C where A has the ".addEtc" and B and C do not know when the other(s) will be changing A so they need listeners... this is NOT the case with my objects, so i probably don't need it anyway, good to know still.
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 05:30 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting