3

so this is the concept: Simply, there's a textbox with "Name" as the value, and I wanted that if I click anywhere IN the textbox, the value "Name" will disappear. This is what I've done in my code:

JTextField t1 = new JTextField("Name", 10);

t1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent cl){
                t1.setText(" ");
            }
        });

There's no syntax errors but when I run the program and clicked somewhere in the textbox, nothing happens and the value "Name" is still there

Any help would be greatly appreciated, thank you!

Rex Endozo
  • 53
  • 1
  • 2
  • 10
  • 1
    This shouldn't even compile. `t1` should be `final` to be accessed in the inner class. – M A Sep 22 '14 at 09:50
  • I accidentally excluded final, sorry for that. But in my program, it is: final JTextField t1 = new JTextField("Name", 10); – Rex Endozo Sep 22 '14 at 09:53
  • 1
    1)`ActionListener` called after you press `ENTER` key on `JTextField` 2)In 1.8 it can be declared without `final`. What you try to achive? – alex2410 Sep 22 '14 at 09:57

3 Answers3

8

You can try this:

t1.addFocusListener(new FocusListener() {

    @Override
    public void focusGained(FocusEvent e) {
        t1.setText(null); // Empty the text field when it receives focus
    }

});
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Mohit
  • 1,185
  • 2
  • 11
  • 25
  • Thanks! This worked for me, I just added the focusLost event to give t1 the value "Name" again which is cool. – Rex Endozo Sep 22 '14 at 10:07
  • Question though, what's the purpose of '@Override'? I removed it and the program runs just fine – Rex Endozo Sep 22 '14 at 10:10
  • @Override annotation in Java is optional but helpful. It defines we are using a new method or overriding an existing method. You can read Method Overriding topic of java to get a clear idea. Thanks – Mohit Sep 22 '14 at 10:17
  • @RexEndozo: See [*Overriding and Hiding Methods*](http://docs.oracle.com/javase/tutorial/java/IandI/override.html). – trashgod Sep 22 '14 at 10:17
  • When I type in a value and then the focus is lost, the textbox sets the value again to "Name" which is bad. How do I make it so that the focusgained t1.setText(null); fire only Once(When the program starts)? – Rex Endozo Sep 22 '14 at 10:44
  • @RexEndozo you can either write what you want to do after focus is lost in public void focusLost(FocusEvent e){} method. if you want your listener to work only once you can try this. public void focusGained(FocusEvent e) { if(t1.getText().equals("Name")) { t1.setText(null); // Empty the text field when it receives focus } else { //do nothing; } } – Mohit Sep 22 '14 at 11:31
3

You shouldn't use an ActionListener for this purpose. Instead of that the FocusListener should work for you, explained here: http://www.java2s.com/Code/JavaAPI/javax.swing/JTextFieldaddFocusListenerFocusListenerl.htm

Lukas Fink
  • 627
  • 6
  • 18
  • I've tried this 't1.addFocusListener(new FocusListener(){ public void focusGained(FocusEvent e){ t1.setText(""); } public void focusLost(FocusEvent e) { } });' but when I run the program, the value "Name" doesn't appear at all – Rex Endozo Sep 22 '14 at 09:57
  • Try using the other method as following: public void focusLos(FocusEvent e) { t1.setText("Name"); } And make the textfield not requesting focus on startup! – Lukas Fink Sep 22 '14 at 09:58
  • @RexEndozo `but when I run the program, the value "Name" doesn't appear at all`, seems that happens because your `JTextField` gain focus at start. – alex2410 Sep 22 '14 at 10:01
0

Don't use ActionListener, it isn't implementing classes for your needs. You can also add MouseListener by:

t1.addMouseListener(new MouseListener(){...});

http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseListener.html

Tomek
  • 543
  • 4
  • 12