0

How can I set text to JTextField when I click off from the component? Whenever I click it, it will clear the text like this:

// Clears the "Enter text here..." when clicked
    commandLine.addMouseListener(new MouseAdapter(){
        @Override
        public void mouseClicked(MouseEvent e){
            commandLine.setText("");
        }
    });  

But, when I click off the textfield, how can I reset it? I tried FocusEvent but it did not work as I wanted.

Timppa
  • 353
  • 2
  • 7
  • 24
  • Are you trying to implement a placeholder? – ollaw May 16 '17 at 15:42
  • I don't know what that really means, I am trying to have a JTextField where is "Enter a message here..." written already, but when you click the JTextField, the message will disappear. After you click off, the same message will be there once again. – Timppa May 16 '17 at 15:44
  • Ok that's a placeholder. If you enter some text, should the example text remain? – ollaw May 16 '17 at 15:46
  • Possible duplicate of http://stackoverflow.com/questions/22396282/how-to-set-text-like-placeholder-in-jtextfield-in-swing and http://stackoverflow.com/questions/16213836/java-swing-jtextfield-set-placeholder – Optional May 16 '17 at 15:46
  • It doesn't matter if it remains or not, because I will use .setText("Enter a message..."); manually so no. – Timppa May 16 '17 at 15:51

1 Answers1

1

I think you just need to add a FocusListener to the textField. Here there is a class I've written that works as you want.

class CustomPlaceholderTextField extends JTextField implements FocusListener {

private static final long serialVersionUID = 1L;
private boolean changedText = false;
private final String placeholder;


public CustomPlaceholderTextField(String placeholder){
    super();
    this.placeholder = Objects.requireNonNull(placeholder);
    this.addFocusListener(this);
    super.setText(placeholder);

}

@Override
public String getText() {
    if (this.changedText) {
        return super.getText();
    } else {
        return "";
    }
}

@Override
public void setText(String t) {
    if (t == null || t.isEmpty()) {
        super.setText(this.placeholder);
        this.changedText = false;
    } else {
        super.setText(t);
        this.changedText = true;
    }
}

@Override
public void focusGained(FocusEvent e) {
    if (!this.changedText) {
        super.setText("");
        this.changedText = true;
    }
}

@Override
public void focusLost(FocusEvent e) {
    if (this.getText().isEmpty()) {
        super.setText(this.placeholder);
        this.changedText = false;
    } else {
        this.changedText = true;
    }
}

}

ollaw
  • 2,086
  • 1
  • 20
  • 33
  • That looks cool, is there a way to add like commandLine.FocusEvent ? like that I do not need to create any different class, just a small method? – Timppa May 16 '17 at 17:16
  • And the problem is that I created my GUi with WindowBuilder (in NetBeans) so I canno't really edit that script much unless I go to properties and edit there, so I rather use some method for that. – Timppa May 16 '17 at 17:43
  • Just add a FocusListener to your textField and implement focusGained() and focusLost() as you want, like i did – ollaw May 16 '17 at 19:46