01: package jimm.datavision.gui;
02:
03: import java.awt.event.ActionEvent;
04: import java.awt.event.ActionListener;
05: import javax.swing.JComponent;
06: import javax.swing.Timer;
07:
08: /**
09: * Gives focus to a component, since often after building a frame the
10: * component we want to have focus doesn't get it.
11: * <p>
12: * 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>.
13: * Modified to perform the focus request only once.
14: *
15: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
16: */
17: public class FocusSetter implements ActionListener {
18:
19: protected static final int WAIT_MILLISECS = 200;
20: protected JComponent component;
21: protected Timer timer;
22:
23: public FocusSetter(JComponent comp) {
24: component = comp;
25: if (component != null) {
26: timer = new Timer(WAIT_MILLISECS, this );
27: timer.setRepeats(false);
28: timer.start();
29: }
30: }
31:
32: public void actionPerformed(ActionEvent evt) {
33: if (evt != null && evt.getSource() == timer
34: && component != null)
35: component.requestFocus();
36: }
37:
38: }
|