for(int i = 0; i < items...
Serwis znalezionych hasełOdnośniki
- Smutek to uczucie, jak gdyby się tonęło, jak gdyby grzebano cię w ziemi.
- object mcal_fetch_current_stream_event ( int stream) Zwraca bieżącą strukturę zdarzenia ze strumienia w postaci obiektu zawierający następujące...
- Operacje wejścia-wyjścia Zmienne typu int, double i char są zmiennymi arytmetycznymi, przystosowanymi odpowiednio do przechowywania...
- int mcrypt_get_key_size ( int cipher) int mcrypt_get_key_size ( string cipher, string module) mcrypt_list_algorithms Funkcja używana do pobrania...
- Zaskoczył mnie...
- sulidory rzadko pojawiały się poza strefą mgieł i Gundersen nie czuł się z nimi swobodnie...
- osunęła się na kolana pośrodku salonu, było głosem śmiertelnie rannego zwierzęcia...
- uświadomiła sobie, że biała postać to kolejny strażnik...
- Nero Cover Designer Adding Fields ⢠103 Choosing a pattern for foreground and background in the field 'Style' using the drop down button...
- Nieraz musiała przystawać po drodze dla otarcia potu z zabrudzonej twarzy i nabrania tchu, zanim doszła do swojego mieszkania, położonego w najbrudniejszej i...
- W
Smutek to uczucie, jak gdyby się tonęło, jak gdyby grzebano cię w ziemi.
length; i++)
t.appendText(items[i] + "\n");
}
else if(evt.target.equals(b)) {
if(count < flavors.length)
lst.addItem(flavors[count++], 0);
}
else
return super.action(evt, arg);
return true;
}
} ///:~
When you press the button it adds items to the top of the list (because of the second argument 0 to addItem( )). Adding elements to a List is more reasonable than the Choice box because users expect to scroll a list box (for one thing, it has a built-in scroll bar) but they don’t expect to have to figure out how to get a drop-down list to scroll, as in the previous example.
However, the only way for action( ) to be called is through a double-click. If you need to monitor other activities that the user is doing on your List (in particular, single clicks) you must take an alternative approach.
handleEvent( )
So far we’ve been using action( ), but there’s another method that gets first crack at everything: handleEvent( ). Any time an event happens, it happens “over” or “to” a particular object. The handleEvent( ) method for that object is automatically called and an Event object is created and passed to handleEvent( ). The default handleEvent( ) (which is 488
Thinking in Java
www.BruceEckel.com
defined in Component, the base class for virtually all the “controls” in the AWT) will call either action( ), as we’ve been using, or other similar methods to indicate mouse activity, keyboard activity, or to indicate that the focus has moved. We’ll look at those later in this chapter.
What if these other methods – action( ) in particular – don’t satisfy your needs? In the case of List, for example, what if you want to catch single mouse clicks but action( ) responds to only double clicks? The solution is to override handleEvent( ) for your applet, which after all is derived from Applet and can therefore override any non-final methods. When you override handleEvent( ) for the applet you’re getting all the applet events before they are routed, so you cannot just assume “This has to do with my button so I can assume it’s been pressed,” since that’s true only for action( ). Inside handleEvent( ) it’s possible that the button has the focus and someone is typing to it. Whether it makes sense or not, those are events that you can detect and act upon in handleEvent( ).
To modify the List example so that it will react to single mouse clicks, the button detection will be left in action( ) but the code to handle the List will be moved into handleEvent( ) as follows:
//: List2.java
// Using lists with handleEvent()
import java.awt.*;
import java.applet.*;
public class List2 extends Applet {
String[] flavors = { "Chocolate", "Strawberry",
"Vanilla Fudge Swirl", "Mint Chip",
"Mocha Almond Fudge", "Rum Raisin",
"Praline Cream", "Mud Pie" };
// Show 6 items, allow multiple selection:
List lst = new List(6, true);
TextArea t = new TextArea(flavors.length, 30);
Button b = new Button("test");
int count = 0;
public void init() {
t.setEditable(false);
for(int i = 0; i < 4; i++)
lst.addItem(flavors[count++]);
add(t);
add(lst);
add(b);
}
public boolean handleEvent(Event evt) {
if(evt.id == Event.LIST_SELECT ||
evt.id == Event.LIST_DESELECT) {
if(evt.target.equals(lst)) {
t.setText("");
String[] items = lst.getSelectedItems();
for(int i = 0; i < items.length; i++)
t.appendText(items[i] + "\n");
}
else
return super.handleEvent(evt);
}
Chapter 13: Creating Windows & Applets
489
else
return super.handleEvent(evt);
return true;
}
public boolean action(Event evt, Object arg) {
if(evt.target.equals(b)) {
if(count < flavors.length)
lst.addItem(flavors[count++], 0);
}
else
return super.action(evt, arg);
return true;
}
} ///:~
The example is the same as before except for the addition of handleEvent( ). Inside, a check is made to see whether a list selection or deselection has occurred. Now remember, handleEvent( ) is being overridden for the applet, so this occurrence could be anywhere on the form and it could be happening to another list. Thus, you must also check to see what the target is. (Although in this case there’s only one list on the applet so we could have made the assumption that all list events must be about that list. This is bad practice since it’s going to be a problem as soon as another list is added.) If the list matches the one we’re interested in, the same code as before will do the trick.