< Day Day Up > |
Focus events are fired whenever a component gains or loses the keyboard focus. This is true whether the change in focus occurs through the mouse, the keyboard, or programmatically. If you're unfamiliar with basic focus concepts or want detailed information about focus, see How to Use the Focus Subsystem (page 583) in Chapter 9. This section explains how to get focus events for a particular component by registering a FocusListener [9] on it. If you're only interested in focus events for windows , you might want to implement a WindowFocusListener instead. If you need to know the focus status of many components , consider implementing a PropertyChangeListener on the KeyboardFocusManager , as described in Tracking Focus Changes to Multiple Components (page 595) in Chapter 9.
Figure 5 demonstrates focus events. Figure 5. The FocusEventDemo application.
Version Note: The focus subsystem was completely rearchitected in release 1.4. This section uses concepts and methods introduced in that release. The window displays a variety of components. A focus listener, registered on each component, reports every focus- gained and focus-lost event. For each event, the other component involved in the focus change, the opposite component , is reported . For example, when the focus goes from a button to a text field, a focus-lost event is fired by the button (with the text field as the opposite component) and then a focus-gained event is fired by the text field (with the button as the opposite component). Focus-lost events can be temporary, which occurs when the window loses the focus, for example. Try This:
You can find the demo's code in FocusEventDemo.java . Here's the code that's related to focus-event handling: public class FocusEventDemo ... implements FocusListener ... { public FocusEventDemo() { ... JTextField textField = new JTextField("A TextField"); textField.addFocusListener(this); ... JLabel label = new JLabel("A Label"); label.addFocusListener(this); ... JComboBox comboBox = new JComboBox(vector); comboBox.addFocusListener(this); ... JButton button = new JButton("A Button"); button.addFocusListener(this); ... JList list = new JList(listVector); list.setSelectedIndex(1); //It's easier to see the focus change //if an item is selected. list.addFocusListener(this); JScrollPane listScrollPane = new JScrollPane(list); //We want to prevent the list's scroll bars from getting the //focus--even with the keyboard. Note that in general we prefer //setRequestFocusable over setFocusable for reasons of //accessibility, but this is to work around bug #4866958. listScrollPane.getVerticalScrollBar().setFocusable(false); listScrollPane.getHorizontalScrollBar().setFocusable(false); ... //Set up the area that reports focus-gained and focus-lost events. display = new JTextArea(); display.setEditable(false); //The method setRequestFocusEnabled prevents a //component from being clickable, but it can still //get the focus through the keyboard - this ensures //user accessibility. display.setRequestFocusEnabled(false); display.addFocusListener(this); JScrollPane displayScrollPane = new JScrollPane(display); //Work around for bug #4866958. displayScrollPane.getHorizontalScrollBar().setFocusable(false); displayScrollPane.getVerticalScrollBar().setFocusable(false); ... } ... public void focusGained(FocusEvent e) { displayMessage("Focus gained", e); } public void focusLost(FocusEvent e) { displayMessage("Focus lost", e); } void displayMessage(String prefix, FocusEvent e) { display.append(prefix + (e.isTemporary() ? " (temporary):" : ":") + e.getComponent().getClass().getName() + "; Opposite component: " + (e.getOppositeComponent != null ? e.getOppositeComponent().getClass().getName() : "null") + newline); } ... } The Focus Listener APITable 13 lists the methods in the FocusListener interface and Table 14 describes the methods in the FocusEvent class. Also refer to the FocusListener API documentation at: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/FocusListener.html. The FocusEvent API documentation is online at: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/FocusEvent.html. Table 13. The FocusListener Interface (The corresponding adapter class is FocusAdapter . [a] )
Table 14. The FocusEvent Class
Examples That Use Focus ListenersThe following examples use focus listeners.
|
< Day Day Up > |