English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

The event that can notify an array list of observors that an action has been made

2007-08-02 11:03:17 · 2 answers · asked by Queen 1 in Computers & Internet Programming & Design

2 answers

You'd code an interface for the notification. For example something like:

public interface EventNotify {
public void eventOccurred( EventData someData );
}

Then you have to do some work in two places:
(1) In the class(es) where you want to notify others of the event.
(2) In the class(es) which want to be notified of the event happening.

On the class where you want to notify others of events, you'd have a method to add an instance of EventNotify to the list of instances to be notified when the event happens, a method to remove an instance of EventNotify from the list, and of course a method to call the eventOccurred() method in every instance of EventNotify registered. Something like:

public interface EventSender {
public void registerForEvents(EventNotify anInstance);
public void unregisterForEvents(EventNotify anInstance);
public void notifyOfEvent(EventData someData);
}

On the class where you want to GET notification, you'd implement the EventNotify interface (in an inner class is probably best), find the instance whose events you want to receive, and register with the class whose events you want to receive:

EventSender es = (something to look up event sender);
EventNotify en = new EventNotify() {
public void eventOccurred( EventData someData ) {
// do something here
} };
es.registerForEvents(en);

2007-08-02 13:02:54 · answer #1 · answered by McFate 7 · 0 0

We can make our applet respond to user input by overriding event handler methods in our applet. There are a variety of event handler methods, as the first example shows. In this demonstration, the applet will display a string, which is modified by each event. The user can move his or her mouse over the applet, click on the applet, and move out of the applet.

Major AWT component events
========================

AWT Event Event signature
mouseDown
mouseDown (Event evt, int x, int y)
mouseUp
mouseUp (Event evt, int x, int y)
mouseEnter (Event evt, int x, int y)
mouseEnter
mouseExit (Event evt, int x, int y)
mouseExit
mouseMove (Event evt, int x, int y)
mouseMove
mouseDrag (Event evt, int x, int y)
mouseDrag
gotFocus gotFocus (Event evt, Object what)
lostFocus lostFocus (Event evt, Object what)
keyDown keyDown (Event evt, int key)
keyUp keyUp (Event evt, int key)
action action (Event evt, Object what)

Each event must return a boolean value (true or false), indicating whether the event should be made available to other event handlers. If you've processed an event (for example, keyDown) you might record the value, and return true to show that no other handlers should receive the event. If, however, your custom edit box can't process the character, it may want to return false to signal that other components (the panel or applet in which the component is hosted) should process it.

Demonstrating AWTEventDemo.java

/*
*
* AWTEventDemo.java
*
*/

import java.awt.*;
import java.applet.*;

public class AWTEventDemo extends Applet
{
private String message = "Waiting for events...";

// Default constructor
public void AWTEventDemo()
{
// Call parent constructor
super();
}

// Init method, called when applet first initialises
public void init()
{
setBackground( Color.white );
}

// Overridden paint method
public void paint ( Graphics g )
{
g.setBackground ( Color.white );
g.setColor ( Color.blue );
g.drawString ( "Hello world!", 0, size().height - 5);
}

// Overridden methods for event handling
public boolean mouseEnter( Event evt, int x, int y)
{
// Set message....
message = "mouseEnter - x:" + x + " y: " + y;

// ... and repaint applet
repaint();

// Signal we have handled the event
return true;
}

public boolean mouseExit( Event evt, int x, int y)
{
// Set message....
message = "mouseExit - x:" + x + " y: " + y;

// ... and repaint applet
repaint();

// Signal we have handled the event
return true;
}

public boolean mouseMove( Event evt, int x, int y)
{
// Set message....
message = "mouseMove - x:" + x + " y: " + y;

// ... and repaint applet
repaint();

// Signal we have handled the event
return true;
}

public boolean mouseDown( Event evt, int x, int y)
{
// Set message....
message = "mouseDown - x:" + x + " y: " + y;

// ... and repaint applet
repaint();

// Signal we have handled the event
return true;
}
}


Creating a custom component - NumberTextField

public boolean keyDown(Event evt, int key)
{
// Cast to a character
char keyChar = (char) key;

// Accept 0-9, . -, backspace or delete
if ( ((keyChar > '0') && (keyChar < '9')) ||
(keyChar == '.') || (keyChar == '-') ||
(keyChar == 8 ) || (keyChar == 127) )
{
// Pass along to our superclass
return super.keyDown(evt, key);
}
else
// Yes we've handled it... by ignoring invalid data
return true;

}

====================================

Creating an applet that responds to component events

The first thing our applet will have to do is create some actual components. This is done in our init method, which is called when the applet is first created. We create an instance of our NumberTextField class, and also a button which when clicked will clear the contents of our numerical text box. Finally, we add our components to the applet container, using a flow layout manager (layout managers control how a container is laid out, but this is beyond the scope of this tutorial).

Next, we add an action event handler, which checks to see if the event's target is equal to our 'clear' button. This is the most critical part of our applet, as we are responding to user events that occur in other components (namely the button). If it is indeed the clear button that has been activated, we clear the contents of the text box, and return true to indicate we've handled the event.




import java.awt.*;

/*
*
* NumberTextField.java
* Customised AWT component
*
*/

public class NumberTextField extends TextField
{
public NumberTextField(int cols)
{
super (cols);
}

public boolean keyDown(Event evt, int key)
{
// Cast to a character
char keyChar = (char) key;

// Accept 0-9, . -, backspace or delete
if ( ((keyChar > '0') && (keyChar < '9')) ||
(keyChar == '.') || (keyChar == '-') ||
(keyChar == 8 ) || (keyChar == 127) )
{
// Pass along to our superclass
return super.keyDown(evt, key);
}
else
// Yes we've handled it... by ignoring invalid data
return true;
}
}

===========================

/*
*
* AWTEventDemo2.java
*
*/

import java.awt.*;
import java.applet.*;

public class AWTEventDemo2 extends Applet
{
// Private references to AWT components
private NumberTextField numberField;
private Button clear;

// Init method, called when applet first initialises
public void init()
{
setBackground( Color.white );

// Set default layout manager
setLayout(new FlowLayout() );

// Create an instance of NumberTextField ....
numberField = new NumberTextField (10);

// ... and add it to our applet
add(numberField);

// Create an instance of button ....
clear = new Button ("Clear");

// ... and add it to our applet
add(clear);
}

public boolean action (Event evt, Object what)
{
// Was the focus of the event our button
if (evt.target == clear)
{
// Clear the textfield
numberField.setText("");

// Event handled
return true;
}
else
return false;
}
}

2007-08-03 07:49:24 · answer #2 · answered by angel04 3 · 1 0

fedest.com, questions and answers