Search code examples
javaswingjbuttonactionlistenerjtextfield

how to clear a jtextfield pushing a button


I have a JButton and when I push it, it has to clear all the content in a JTextfield. How can I do this? Maybe there exists a method to clear a JTextfield or do I have to set the field to null?


Solution

  • The JTextField#setText method should do it with an empty String as text.

    As for reacting on the JButton click, attach an ActionListener to it. The Swing tutorials illustrate this all in great detail.

    1. Button tutorial
    2. Text component tutorial
    3. ActionListener tutorial