1 package org.glassbox.gui;
2
3 import java.awt.event.FocusListener;
4 import java.awt.event.FocusEvent;
5 import java.awt.event.FocusAdapter;
6 import javax.swing.JTextField;
7
8 /***
9 A extension of the JTextField. This implementation resets its text automatically
10 on a focus-lost event to the previous value.
11 */
12 public class AutoResetTextField extends JTextField {
13 public interface TextProvider {
14 /***
15 Get the valid text to put into the text field
16 when a focus-lost event has been received by
17 the textfield.
18 */
19 String getValidText();
20 }
21
22 private FocusListener FOCUS_LISTENER = new FocusAdapter() {
23 public void focusLost(FocusEvent e) {
24 setText(_provider.getValidText());
25 }
26 };
27
28 private TextProvider _provider;
29
30 public AutoResetTextField(TextProvider provider) {
31 super(provider.getValidText());
32 _provider = provider;
33 addFocusListener(FOCUS_LISTENER);
34 }
35 }