KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > FocusSetter


1 package jimm.datavision.gui;
2 import java.awt.event.ActionEvent JavaDoc;
3 import java.awt.event.ActionListener JavaDoc;
4 import javax.swing.JComponent JavaDoc;
5 import javax.swing.Timer JavaDoc;
6
7 /**
8  * Gives focus to a component, since often after building a frame the
9  * component we want to have focus doesn't get it.
10  * <p>
11  * Based on code found at <a HREF="http://privat.schlund.de/b/bossung/prog/java/tips.html">http://privat.schlund.de/b/bossung/prog/java/tips.html</a>.
12  * Modified to perform the focus request only once.
13  *
14  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
15  */

16 public class FocusSetter implements ActionListener JavaDoc {
17
18 protected static final int WAIT_MILLISECS = 200;
19 protected JComponent JavaDoc component;
20 protected Timer JavaDoc timer;
21
22 public FocusSetter(JComponent JavaDoc comp) {
23     component = comp;
24     if (component != null) {
25     timer = new Timer JavaDoc(WAIT_MILLISECS, this);
26     timer.setRepeats(false);
27     timer.start();
28     }
29 }
30
31 public void actionPerformed(ActionEvent JavaDoc evt) {
32     if (evt != null && evt.getSource() == timer && component != null)
33     component.requestFocus();
34 }
35
36 }
37
Popular Tags