3
\$\begingroup\$

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;
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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);
    }
}
  1. 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.

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

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

  4. 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);
    }
}
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.