tzelve
asked on
creating events.
i would create an event to a class.I think this will be happen with some listener but all listener that java have is only for swing or awt.i would create an event ex when a variable change.My project is that. I have a shoket to communicate with some other pc's .To make connection i use some buffereader and input streame objects.I like to take an event when the data have some prices example 23 or "password:23".
If you have some idea who i can make a non grafical event tell me.Thanks you very much!
If you have some idea who i can make a non grafical event tell me.Thanks you very much!
listening...:)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ovi,
This is another way to solve this problem by using Observer/Observable pattern..... ;-))
tzelve,
If there is an state change you have to notify some other object then you have to use Observer/Observable pattern. In java 1.1 & 1.2 event handling is based on this pattern only.
In AWT all the Events (i.e WindowEvent,ActionEvent.. ) class are Observable which will tell the all observers (itnterested parties) that the state of the object was changed. (All the Listeners are Observers)
In java by using Oberver and Observable class in java.util Package we will achive it.
Some days back I grabbed this code form net. Go through this you will get some ideas about Observer/Observable pattern.
-------------------------- ---------- ---------- ---------- --
The Observer/Observable pattern may be appropriate in the situation where you have need to update a set of objects based on a change in another. At least two objects are needed, one which extends the Observable class and represents the changeable 'data' or 'model' and one that implements the Observer interface and is the class whose interest is registered with the Observable. The following is a short demo - the only code of real import is the implementation of the update() method by the Observer, and the use of the three methods addObserver(),setChanged() , and notifyObservers() [the minimum that must be used] by the Observable.
Example:-
A change in the state of a String in the Observable will notify the Observer, which will in turn output a message to that effect to the console:
import java.util.*;
import java.io.*;
public class Actor extends Observable {
private String stateString = "idle";
public Actor() {
addObserver(new Watcher());
}
public String getState() {
return stateString;
}
public void setState(String s) {
if( ! (s.equals(stateString) ) ) {
setChanged();
notifyObservers("State changed");
}
stateString = s;
}
public static void main(String[] args) {
String s;
Actor actor = new Actor();
BufferedReader reader =
new BufferedReader(
new InputStreamReader(System.i n));
try {
System.out.println("Enter a state (" +
actor.getState(
) + " is the current state): ");
while((s = reader.readLine()) != null) {
actor.setState(s);
System.out.println("Curren t state: " +
actor.getState());
}
}
catch(Exception e) {
System.out.println("Progra m terminated.");
}
}
}
class Watcher implements Observer {
public void update(Observable o, Object arg) {
System.out.println("Observ able object " +
o + " has changed state.");
}
}
-------------------------- ---------- ---------- ---------- ---
....dviji
This is another way to solve this problem by using Observer/Observable pattern..... ;-))
tzelve,
If there is an state change you have to notify some other object then you have to use Observer/Observable pattern. In java 1.1 & 1.2 event handling is based on this pattern only.
In AWT all the Events (i.e WindowEvent,ActionEvent.. ) class are Observable which will tell the all observers (itnterested parties) that the state of the object was changed. (All the Listeners are Observers)
In java by using Oberver and Observable class in java.util Package we will achive it.
Some days back I grabbed this code form net. Go through this you will get some ideas about Observer/Observable pattern.
--------------------------
The Observer/Observable pattern may be appropriate in the situation where you have need to update a set of objects based on a change in another. At least two objects are needed, one which extends the Observable class and represents the changeable 'data' or 'model' and one that implements the Observer interface and is the class whose interest is registered with the Observable. The following is a short demo - the only code of real import is the implementation of the update() method by the Observer, and the use of the three methods addObserver(),setChanged()
Example:-
A change in the state of a String in the Observable will notify the Observer, which will in turn output a message to that effect to the console:
import java.util.*;
import java.io.*;
public class Actor extends Observable {
private String stateString = "idle";
public Actor() {
addObserver(new Watcher());
}
public String getState() {
return stateString;
}
public void setState(String s) {
if( ! (s.equals(stateString) ) ) {
setChanged();
notifyObservers("State changed");
}
stateString = s;
}
public static void main(String[] args) {
String s;
Actor actor = new Actor();
BufferedReader reader =
new BufferedReader(
new InputStreamReader(System.i
try {
System.out.println("Enter a state (" +
actor.getState(
) + " is the current state): ");
while((s = reader.readLine()) != null) {
actor.setState(s);
System.out.println("Curren
actor.getState());
}
}
catch(Exception e) {
System.out.println("Progra
}
}
}
class Watcher implements Observer {
public void update(Observable o, Object arg) {
System.out.println("Observ
o + " has changed state.");
}
}
--------------------------
....dviji
Hi,
I agree with objects that PropertyChangeEvent/Listen er is correct way to reflect variable change. Note that package is java.beans. Look at java.beand.ProperyChangeSu pport class--it does a lot of work for listener notification and registering.
Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
I agree with objects that PropertyChangeEvent/Listen
Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
ASKER
Your example is simple dviji, but i have this problem:
If the variable is backround changable?
My project is that i use a Socket object to communicate with some other pc and i want to listen datas, when the datas are equal with costand value to call an event.in your example you must run the notifyObservers() method to work.
Help me if you can...
If the variable is backround changable?
My project is that i use a Socket object to communicate with some other pc and i want to listen datas, when the datas are equal with costand value to call an event.in your example you must run the notifyObservers() method to work.
Help me if you can...
To tzelve : Thanks for the points
To dviji : I use the Observer/Observable pattern only and only in implementing the MVC pattern (model-view-controller). Will work of course in the case of SIMULATING the event notification but is not a pure solution. I believe SOME TIME the things must be used for what they have been created for. Any way thanks for details.
http://www.google.com/search?hl=en&safe=off&q=MVC+applet
To dviji : I use the Observer/Observable pattern only and only in implementing the MVC pattern (model-view-controller). Will work of course in the case of SIMULATING the event notification but is not a pure solution. I believe SOME TIME the things must be used for what they have been created for. Any way thanks for details.
http://www.google.com/search?hl=en&safe=off&q=MVC+applet
You'll need to create an addPropertyChangeListener and removePropertyChangeListen
And when a property change occurs in your object then you'll need to send a PropertyChangeEvent to each registered listener.