Skip to content
Advertisement

Listener only for the first click on JTextField

Is there a java event for JTextField do like : if the cursor already was in the JTextField and MousePressed = do nothing if click on the JTextField for the first time = do things I have been used , MousePressed(java.awt.event.MouseEvent evt) but this not exactly what I want. thank you

Advertisement

Answer

You can implement your own custom logic:

  • Store a “flag” like global boolean variable:
    boolean alreadyClicked = false
    
  • Each time you click the JTextField check (and toggle) that variable:
    if(!alreadyClicked) {
       alreadyClicked = true;
      // do your work
    }
    
  • When focusing out from that field toggle variable again to:
    alreadyClicked = false;
    
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement