This allows the creation of a control that appears as a simple label until clicked, at which point it becomes editable. This class also provides a mouseover and cursor affordance when the user hovers over the label.
To use this class, first ensure that the "viewer" (label) is visible and that the "editor" (text field) is not. Then create an EditableLabelListener for the viewer and editor. It is not necessary to attach this class as a listener to either the viewer or the editor, as this is done automatically in the constructor.
Public Member Functions | |
EditableLabelListener (Component parent, JLabel viewer, JTextField editor) | |
Constructs an EditableLabelListener with the given parent, viewer, and editor components. | |
void | actionPerformed (ActionEvent e) |
Called when the user hits the return key inside the text field. | |
void | focusGained (FocusEvent e) |
Does nothing when the focus is gained. | |
void | focusLost (FocusEvent e) |
Replaces the editor with the viewer when the focus of the editor is lost. | |
void | mouseClicked (MouseEvent e) |
Replaces the viewer with the editor when the mouse is clicked in the viewer. | |
void | mouseEntered (MouseEvent e) |
Displays a mouseover border on the viewer when the mouse is moved into it. | |
void | mouseExited (MouseEvent e) |
Removes the mouseover border from the viewer when the mouse is moved off of it. | |
Static Public Attributes | |
Borders | |
Border styles for the label. | |
static final EmptyBorder | NORMAL_LABEL_BORDER = new EmptyBorder(3, 3, 3, 3) |
The normal label border, which is just empty with the same insets as the text field. | |
static final CompoundBorder | MOUSEOVER_LABEL_BORDER |
The mouseover label border, which draws a translucent black line around the edge of the label. | |
Protected Member Functions | |
String | fieldTransform (String fieldText) |
Returns the string that should be transferred to the viewer component when the editor component is done being edited. | |
String | labelTransform (String labelText) |
Returns the string that should be transferred from the viewer component to the editor component when the editor component is shown. | |
Private Attributes | |
Component | parent |
The component that contains the viewer and editor. | |
JLabel | viewer |
The viewer component. | |
JTextField | editor |
The editor component. |
edu.cmu.hcii.calo.view.EditableLabelListener.EditableLabelListener | ( | Component | parent, | |
JLabel | viewer, | |||
JTextField | editor | |||
) |
Constructs an EditableLabelListener with the given parent, viewer, and editor components.
The viewer must be a JLabel, and the editor must be a JTextField. The parent is only necessary to determine whether the interaction should be enabled.
parent | the parent component of the editor and viewer | |
viewer | the viewer component | |
editor | the editor component |
void edu.cmu.hcii.calo.view.EditableLabelListener.actionPerformed | ( | ActionEvent | e | ) |
Called when the user hits the return key inside the text field.
e | the action event |
String edu.cmu.hcii.calo.view.EditableLabelListener.fieldTransform | ( | String | fieldText | ) | [protected] |
Returns the string that should be transferred to the viewer component when the editor component is done being edited.
This is implemented simply to return the contents of the editor, but subclasses can override it to define custom behavior (like changing the empty string into a placeholder for the label).
fieldText | the text that was placed into the editor |
void edu.cmu.hcii.calo.view.EditableLabelListener.focusGained | ( | FocusEvent | e | ) |
Does nothing when the focus is gained.
e | the focus event |
void edu.cmu.hcii.calo.view.EditableLabelListener.focusLost | ( | FocusEvent | e | ) |
Replaces the editor with the viewer when the focus of the editor is lost.
e | the focus event |
String edu.cmu.hcii.calo.view.EditableLabelListener.labelTransform | ( | String | labelText | ) | [protected] |
Returns the string that should be transferred from the viewer component to the editor component when the editor component is shown.
This is implemented simply to return the contents of the viewer, but subclasses can override it to define custom behavior (like changing some placeholder into the empty string for the purposes of editing).
labelText | the text that is being shown in the viewer |
void edu.cmu.hcii.calo.view.EditableLabelListener.mouseClicked | ( | MouseEvent | e | ) |
Replaces the viewer with the editor when the mouse is clicked in the viewer.
e | the mouse event |
void edu.cmu.hcii.calo.view.EditableLabelListener.mouseEntered | ( | MouseEvent | e | ) |
Displays a mouseover border on the viewer when the mouse is moved into it.
e | the mouse event |
void edu.cmu.hcii.calo.view.EditableLabelListener.mouseExited | ( | MouseEvent | e | ) |
Removes the mouseover border from the viewer when the mouse is moved off of it.
e | the mouse event |
final EmptyBorder edu.cmu.hcii.calo.view.EditableLabelListener.NORMAL_LABEL_BORDER = new EmptyBorder(3, 3, 3, 3) [static] |
The normal label border, which is just empty with the same insets as the text field.
final CompoundBorder edu.cmu.hcii.calo.view.EditableLabelListener.MOUSEOVER_LABEL_BORDER [static] |
Initial value:
new CompoundBorder(new LineBorder(new Color(0f, 0f, 0f, 0.5f)), new EmptyBorder(2, 2, 2, 2))
Component edu.cmu.hcii.calo.view.EditableLabelListener.parent [private] |
The component that contains the viewer and editor.
JLabel edu.cmu.hcii.calo.view.EditableLabelListener.viewer [private] |
The viewer component.
JTextField edu.cmu.hcii.calo.view.EditableLabelListener.editor [private] |
The editor component.