Skip to main content

APIzation: Replication Package

[Q#10133366][A#10133538] How to clear JTextField when mouse clicks the JTextField

https://stackoverflow.com/q/10133366

I need to make this program clear the text from the text field when the mouse clicks in that text field. I have tried a few things, but none of them have yet to work for me. Here is the code in its entirety:

Answer

https://stackoverflow.com/a/10133538

TL;DR Anyway, registering a MouseAdapter and overriding mouseClicked worked for me, I hope this example gets you started in the right direction!

APIzation

import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class ClickAndClearDemo {
    private static void createAndShowGUI(){
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));

        final JTextField textField = new JTextField("Enter text here...");
        textField.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){
                textField.setText("");
            }
        });

        frame.add(textField);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
package com.stackoverflow.api;

import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Human10133538 {

  private static void createAndShowGUI() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));

    final JTextField textField = new JTextField("Enter text here...");
    textField.addMouseListener(
      new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
          textField.setText("");
        }
      }
    );

    frame.add(textField);
    frame.pack();
    frame.setVisible(true);
  }

  public static void clickAndClearDemo() {
    SwingUtilities.invokeLater(
      new Runnable() {
        @Override
        public void run() {
          createAndShowGUI();
        }
      }
    );
  }
}

package com.stackoverflow.api;

import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

/**
 * How to clear JTextField when mouse clicks the JTextField
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/10133538">https://stackoverflow.com/a/10133538</a>
 */
public class APIzator10133538 {

  private static void createAndShowGUI() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
    final JTextField textField = new JTextField("Enter text here...");
    textField.addMouseListener(
      new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
          textField.setText("");
        }
      }
    );
    frame.add(textField);
    frame.pack();
    frame.setVisible(true);
  }

  public static void clearJtextfield() {
    SwingUtilities.invokeLater(
      new Runnable() {
        @Override
        public void run() {
          createAndShowGUI();
        }
      }
    );
  }
}