HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Text box intuitive user-interaction using focus listeners

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
usertextfocusinteractionintuitiveusinglistenersbox

Problem

This code aims to replace labels used to explain a text box's container. This is done by adding a default string when the text box is considered empty to explain the purpose of the text box. However, when the text box is not considered empty, the text box will behave normally.

This is what I have written to solve my issue, I am posting it here for feedback and tips regarding anything to do with my code.

private void jTextFieldFocusGained(FocusEvent evt) {
    if(jTextField.getText().equals(TEXT_DEFAULT))
    {
        jTextField.setForeground(FOCUSED_COLOR);
        jTextField.setText(TEXT_FOCUSED);
    }
}
private void jTextFieldFocusLost(FocusEvent evt) {
    if(jTextField.getText().equals(TEXT_FOCUSED))
    {
        jTextField.setText(TEXT_DEFAULT);
        jTextField.setForeground(COLOR_DEFAULT);
    }
}
private final String TEXT_FOCUSED = "";
private final String TEXT_DEFAULT = "Type text here...";
private final Color COLOR_FOCUSED = Color.BLACK;
private final Color COLOR_DEFAULT = Color.LIGHT_GRAY;

Solution

It is in fact pretty good code, just some small nitpicks:
Order

The fields should go before the methods; in fact, it should be near the front of the class, only after the public static final and private static final fields.
Conventions

This piece of code:

private void jTextFieldFocusGained(FocusEvent evt) {
    if(jTextField.getText().equals(TEXT_DEFAULT))
    {
        jTextField.setForeground(FOCUSED_COLOR);
        jTextField.setText(TEXT_FOCUSED);
    }
}
private void jTextFieldFocusLost(FocusEvent evt) {
    if(jTextField.getText().equals(TEXT_FOCUSED))
    {
        jTextField.setText(TEXT_DEFAULT);
        jTextField.setForeground(COLOR_DEFAULT);
    }
}


-
In the if statement, if you don't put a new line before the method brace, you shouldn't put a new line before the if brace.

-
You are inconsistent in order. You set the foreground color first in one, and the text in another. Be consistent.

-
You should put 2 new lines between methods, and any non-related lines.

-
It is recommended to have a space after the if.

Naming

evt should be event: names are concise names that shouldn't be abbreviated.

jTextField could just be textField. I know you are using a JTextField, but textField is a more readable name.
Final Code:

private final String TEXT_FOCUSED = "";
private final String TEXT_DEFAULT = "Type text here...";
private final Color COLOR_FOCUSED = Color.BLACK;
private final Color COLOR_DEFAULT = Color.LIGHT_GRAY;

private void textFieldFocusGained(FocusEvent event) {
    if (textField.getText().equals(TEXT_DEFAULT)) {
        textField.setForeground(FOCUSED_COLOR);
        textField.setText(TEXT_FOCUSED);
    }
}

private void textFieldFocusLost(FocusEvent event) {
    if (textField.getText().equals(TEXT_FOCUSED)) {
        textField.setForeground(COLOR_DEFAULT);
        textField.setText(TEXT_DEFAULT);
    }
}

Code Snippets

private void jTextFieldFocusGained(FocusEvent evt) {
    if(jTextField.getText().equals(TEXT_DEFAULT))
    {
        jTextField.setForeground(FOCUSED_COLOR);
        jTextField.setText(TEXT_FOCUSED);
    }
}
private void jTextFieldFocusLost(FocusEvent evt) {
    if(jTextField.getText().equals(TEXT_FOCUSED))
    {
        jTextField.setText(TEXT_DEFAULT);
        jTextField.setForeground(COLOR_DEFAULT);
    }
}
private final String TEXT_FOCUSED = "";
private final String TEXT_DEFAULT = "Type text here...";
private final Color COLOR_FOCUSED = Color.BLACK;
private final Color COLOR_DEFAULT = Color.LIGHT_GRAY;

private void textFieldFocusGained(FocusEvent event) {
    if (textField.getText().equals(TEXT_DEFAULT)) {
        textField.setForeground(FOCUSED_COLOR);
        textField.setText(TEXT_FOCUSED);
    }
}

private void textFieldFocusLost(FocusEvent event) {
    if (textField.getText().equals(TEXT_FOCUSED)) {
        textField.setForeground(COLOR_DEFAULT);
        textField.setText(TEXT_DEFAULT);
    }
}

Context

StackExchange Code Review Q#108349, answer score: 2

Revisions (0)

No revisions yet.