Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement