Search code examples
javaswingtextfieldjtextfield

How can I clear the textfield when it is enabled?


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!


Solution

  • 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...