0

The textfield is disabled and with text.

When I touch a button, the textfield will be enabled for editing.

However, I would like to textfield to be cleared when enabled.

One constraint is that I can;t add any other listener to the button.

Is there anyone can answer my question?

Many Thanks!

Garis M Suero
  • 7,974
  • 7
  • 45
  • 68
William LAM
  • 445
  • 4
  • 20

3 Answers3

2

Try this..

Inside the button listener that enables the textfield, after enabling statement,just add mytext.setText("");

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
1

You could attach a property listener and listen for the "enabled" event

field.addPropertyChangeListener("enabled", listener);

This doesn't require you mess around with the field at all...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks for your answer, I have tried textField.addPropertyChangeListener("enabled", new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { text.setText(""); panel.revalidate(); } }); it seems that the program didn't run into the listener when the textfield is enabled. – William LAM Jul 10 '12 at 06:03
  • Problem solved!! field.addPropertyChangeListener("editable", listener); Thx!! – William LAM Jul 10 '12 at 06:31
0

check here, Here is the answer of your question. You just need some modifications...

Java JTextField with input hint

Community
  • 1
  • 1
djhs16
  • 471
  • 1
  • 5
  • 12
  • I'm sorry. I am not having a listener that listner to the value change of the textfield. I would like to clear the textfield when it is being enabled by the button. And I can't do anything on Button is the constraint. – William LAM Jul 10 '12 at 06:04