1

Is it possible that when I clicked the textfield it would clear the recent text that was inputed there?. Mine was like, suppose these are textfields.

   Name: Last Name     First Name      Middle Initial

Then I would click the Last Name and it would be cleared, same as First Name and Middle Initial. thanks for reading, hope you can help me.

PeakGen
  • 21,894
  • 86
  • 261
  • 463
user1694994
  • 55
  • 1
  • 3
  • 4

3 Answers3

6

Consider a FocusListener, one where all the text is selected:

myTextField.addFocusListener(new FocusAdapter() {
  public void focusGained(FocusEvent fEvt) {
    JTextField tField = (JTextField)fEvt.getSource();
    tField.selectAll();
  }
});

By selecting all of the text, you give the user the option of either typing and thus deleting the current text and replacing it with the new text, or using the mouse or arrow keys to keep the current text and possibly change it.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    +1 for considering `Help users recognize, diagnose, and recover from errors` ( Ten Usability Heuristics : http://www.useit.com/papers/heuristic/heuristic_list.html ) – user1406062 Oct 07 '12 at 04:10
1

I think Hovercraft is right. Better to use a FocusListener for this purpose.

I would write a utility class that could deal with this, I've done something similar for auto select. Means I don't have to extend every text component that comes along or mess around with lost of small focus listeners that do the same thing.

public class AutoClearOnFocusManager extends FocusAdapter {

    private static final AutoClearOnFocusManager SHARED_INSTANCE = new AutoClearOnFocusManager();

    private AutoClearOnFocusManager() {

    }

    public static AutoClearOnFocusManager getInstance() {
        return SHARED_INSTANCE;
    }

    @Override
    public void focusGained(FocusEvent e) {
        Component component = e.getComponent();
        if (component instanceof JTextComponent) {
            ((JTextComponent)component).setText(null);
        }
    }

    public static void install(JTextComponent comp) {
        comp.addFocusListener(getInstance());
    }

    public static void uninstall(JTextComponent comp) {
        comp.removeFocusListener(getInstance());
    }        
}

Then you just need to use

JTextField textField = new JTextField("Some text");
AutoClearOnFocusManager.install(textField);

If you're just looking to supply a "prompt" (text inside the field that prompts the user), you could also look at the Prompt API

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Why don't use the mouseClicked event?

So, you can have something like

jTextFieldMyText.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        jTextFieldMyTextMouseClicked(evt);
    }
});

private void jTextFieldMyTextMouseClicked(java.awt.event.MouseEvent evt) {
    jTextFieldMyText.setText("");
}

In the case of focus

jTextFieldMyText.addFocusListener(new java.awt.event.FocusAdapter() {
    public void focusGained(java.awt.event.FocusEvent evt) {
        jTextFieldMyTextFocusGained(evt);
    }
});

private void jTextFieldMyTextFocusGained(java.awt.event.MouseEvent evt) {
    jTextFieldMyText.setText("");
}

If deleting text inmediatelly isn't what's wanted, use selectAll() instead of setText("") as suggested many times

mishamosher
  • 1,003
  • 1
  • 13
  • 28