0

I'm trying to make an interface for a login/register app and I have, in the email box (jTextField) an example as text (example@gmail.com) but when I run my program when I click that box to write my email on it, I have to delete my set text to write what I want.

What I thought to do was to create 2 jTextFields, the one behind not editable and the one forward where I'd put my text. So there are two things I don't know how to do:

  1. put the forward jTextField invisible so we can see the behind one
  2. make the text on the behind jTextField disappear when I click the front one

Thanks for trying the help.

Ank
  • 1,864
  • 4
  • 31
  • 51
Marta Azoia
  • 25
  • 1
  • 5
  • 1
    Possible duplicate of [Java - placeholder on textfield](https://stackoverflow.com/questions/13033600/java-placeholder-on-textfield) – juzraai Sep 16 '17 at 10:01

1 Answers1

0

Can easily done with FocusGained and focuseLost events

private void txtEmailFocusGained(java.awt.event.FocusEvent evt) {                                     
    if (txtEmail.getText().equals("example@example.com")) {
        txtEmail.setText(null);
    }
}     


private void txtEmailFocusLost(java.awt.event.FocusEvent evt) {                                   
   if ( txtEmail.getText().equals("")) {
        txtEmail.setText("example@example.com");
    }
}         
ThilinaMD
  • 365
  • 3
  • 13