1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.commons.gui;
21
22 import java.awt.event.ActionEvent;
23 import javax.swing.Action;
24 import javax.swing.JTextField;
25 import org.xnap.commons.gui.action.AbstractXNapAction;
26 import org.xnap.commons.i18n.I18nFactory;
27
28 /***
29 * Erases the text of a {@link javax.swing.JTextField JTextField}.
30 */
31 public class EraseTextFieldAction extends AbstractXNapAction {
32
33 private JTextField textField;
34
35 /***
36 * Constructs an erase action, that has its text field set to
37 * <code>jtf</code>.
38 *
39 * @param textField the text field to erase
40 */
41 public EraseTextFieldAction(JTextField textField)
42 {
43 this.textField = textField;
44
45 putValue(Action.SHORT_DESCRIPTION,
46 I18nFactory.getI18n(EraseTextFieldAction.class).tr("Erases text"));
47 putValue(ICON_FILENAME, "locationbar_erase.png");
48 }
49
50 /***
51 * Constructs an erase action, that has no text field set. Use
52 * {@link #setTextField(JTextField) setTextField} to set a text field.
53 */
54 public EraseTextFieldAction()
55 {
56 this(null);
57 }
58
59 /***
60 * Erases the text field.
61 */
62 public void actionPerformed(ActionEvent event)
63 {
64 if (textField != null) {
65 textField.setText("");
66 textField.grabFocus();
67 }
68 }
69
70 public JTextField getTextField()
71 {
72 return textField;
73 }
74
75 /***
76 * Sets the text field to <code>newValue</code>.
77 */
78 public void setTextField(JTextField newValue)
79 {
80 textField = newValue;
81 }
82
83 }