0

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Alex
  • 2,145
  • 6
  • 36
  • 72
  • 1
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson May 01 '15 at 05:55
  • @Alex please where is multiple `jtext2.addMouseListener(new MouseAdapter(){` removed, JTextField thoeretically cat to returns null, isEmpty or (sequence) of Char(s) – mKorbel May 01 '15 at 07:01
  • @mKorbel Not sure I understand your question? I do have another instance of `jtext2.addMouseListener(new MouseAdapter(){` for another error message dependent on a separate failure state. – Alex May 01 '15 at 07:51
  • @Alex which of `jtext2.addMouseListener(new MouseAdapter()` (clearing text on mouse click frommultiple jtextfields) you are really listening, because you are in risk that there can be milions `jtext2.addMouseListener(new MouseAdapter()` added to one `JTextField` at runtime :-) – mKorbel May 01 '15 at 09:16

1 Answers1

2

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.

Community
  • 1
  • 1
Sourc
  • 36
  • 1
  • 4
  • Thank you for the solution! Just curious though, if I have a JTextField already, like `JTextField text1 = new JTextField();` How could I convert it to the `HintTextField`? Or do I just use `HintTextField` for everything? Once a user enters something invalid I tried doing `text1 = new HintTextField("error text");` bu it told me it can't convert from `HintTextField` to `javax.swing.JTextField` – Alex May 01 '15 at 20:20
  • @Alex When you initialize your text fields, you use JTextField text1 = HintTextField("Message"). After that, the "Message" will display every time you don't have any text inside the text field, and when the field isn't focused. – Sourc May 01 '15 at 20:27