Search code examples
javaswingfocusjtextfieldmouselistener

Clearing text on mouse click from multiple JTextFields in Java


I'm doing some error checking in two JTextFields. I'll label them jtext1 and jtext2.

Right now my GUI looks like this:

Enter number 1: [      ] // jtext1
Enter number 2: [      ] // jtext2
Convert         [ answer ]

I want to be able to check for multiple conditions and display an error message. For example, if the user enters nothing for both text fields, and hits convert the following should appear:

Enter number 1: [  Nothing entered    ] // jtext1
Enter number 2: [  Nothing entered    ] // jtext2
Convert         [ answer ]

And, if you use the mouse to click on jtext1 I want it to clear the message so the user can enter a number. If they click on jtext2 the same thing should also happen.

So for example if I were to click on jtext2 the following should appear:

Enter number 1: [  Nothing entered    ] // jtext1
Enter number 2: [        ]              // jtext2
Convert         [ answer ]

The problem is, with this piece of code:

if(!jtext1.isvalid()) {
    jtext1.setText("Nothing entered");
    jtext1.addMouseListener(new MouseAdapter(){
        @Override
        public void mouseClicked(MouseEvent e) {
            jtext1.setText("");
        }
    });
}

if(!jtext2.isvalid()) {
    jtext2.setText("Nothing entered");
    jtext2.addMouseListener(new MouseAdapter(){
        @Override
        public void mouseClicked(MouseEvent e) {
            jtext2.setText("");
        }
    });
}

Only jtext1 will work. This sort of makes sense to me, because I don't think I can keep overriding the same function, but how would I go around the issue?


Solution

  • I think it would be better if you create another class that can display a hint, that way it will be easier whenever you need to do this again. Here's an example of how it could work, taken from another question on SO

    class HintTextField extends JTextField implements FocusListener {
    
      private final String hint;
      private boolean showingHint;
    
      public HintTextField(final String hint) {
        super(hint);
        this.hint = hint;
        this.showingHint = true;
        super.addFocusListener(this);
      }
    
      @Override
      public void focusGained(FocusEvent e) {
        if(this.getText().isEmpty()) {
          super.setText("");
          showingHint = false;
        }
      }
      @Override
      public void focusLost(FocusEvent e) {
        if(this.getText().isEmpty()) {
          super.setText(hint);
          showingHint = true;
        }
      }
    
      @Override
      public String getText() {
        return showingHint ? "" : super.getText();
      }
    }
    

    You can use this and then set your JTextField to new HintTextField("What you want your hint to be"). You can modify this class for your own needs, but it's a good starting point.

    Update: And if you want to use your current code for whatever reason, change the mouse listener to a focus listener and override focusGained.