SHARE
TWEET

Untitled

a guest May 23rd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.Color;
  2. import java.awt.event.FocusEvent;
  3. import java.awt.event.FocusListener;
  4.  
  5. import javax.swing.JTextField;
  6.  
  7. public class JHintTextField extends JTextField implements FocusListener{
  8.    
  9.     private String hint;
  10.    
  11.     public JHintTextField() {
  12.         this.addFocusListener(this);
  13.     }
  14.  
  15.     @Override
  16.     public void focusGained(FocusEvent e) {
  17.         if(this.getText().toString().equals(hint)) {
  18.             this.setText("");
  19.             this.setForeground(Color.BLACK);
  20.         }
  21.     }
  22.  
  23.     @Override
  24.     public void focusLost(FocusEvent e) {
  25.         if(this.getText().equals("")) {
  26.             this.setHintText(this.hint);
  27.         }
  28.     }
  29.  
  30.     public void setHintText(String hint) {
  31.         this.hint = hint;
  32.         this.setText(hint);
  33.         this.setForeground(Color.LIGHT_GRAY);
  34.     }
  35. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top