How to clear JTextField when mouse clicks the JTextField

69,853

Solution 1

TL;DR

Anyway, registering a MouseAdapter and overriding mouseClicked worked for me,

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();
            }
        });
    }
}

I hope this example gets you started in the right direction!

Solution 2

Is it to clear the 'hint' text?

I think this is what you're trying to do...

textField.addMouseListener(new MouseAdapter())
    {
        public void mouseClicked(MouseEvent e)
        {
            if(textField.getText().equals("Default Text"))
            {
                textField.setText("");
                repaint();
                revalidate();
            }           
        }
    });

Solution 3

This worked for me. Of course, the text is cleared when you click, and you can enter new text. To clear the text again via a click, the textfield has to lose focus and then regain focus from the mouse. I am not entirely sure what you are looking for here.

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class ClickTextField extends JTextField implements MouseListener{

public static void main(String[] args) {
    new ClickTextField();
}

public ClickTextField() {
    addMouseListener(this);

    JFrame J = new JFrame();
    J.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    J.setSize(100,100);
    J.getContentPane().add(this);
    setText("Texty text...");
    J.show();
}

@Override
public void mouseClicked(MouseEvent e) {

    setText("");

}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

}
Share:
69,853
Joshua Vaughan
Author by

Joshua Vaughan

Updated on July 02, 2022

Comments

  • Joshua Vaughan
    Joshua Vaughan almost 2 years

    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:

    public class TimerClassPanel extends JFrame implements MouseListener{
    
        public TimerClassPanel(){
            setTitle("Timer Class");
            setSize(WIDTH, HEIGHT);
    
            timer = new Timer(DELAY, new TimerEventHandler());
    
            pane = getContentPane();
            pane.setLayout(null);
    
            int r = (int)(9.0 * Math.random()) + 1;
            String str2 = Integer.toString(r);
    
            label = new JLabel(str2, SwingConstants.CENTER);
            label.setSize(150,30);
            label.setLocation(0,0);
    
            textField = new JTextField();
            textField.setSize(150,30);
            textField.setLocation(150,0);
    
            startB = new JButton("Start");
            startbh = new StartButtonHandler();
            startB.addActionListener(startbh);
            startB.setSize(100,30);
            startB.setLocation(0,30);
    
            stopB = new JButton("Stop");
            stopbh = new StopButtonHandler();
            stopB.addActionListener(stopbh);
            stopB.setSize(100,30);
            stopB.setLocation(100,30);
    
            exitB = new JButton("Exit");
            ebHandler = new ExitButtonHandler();
            exitB.addActionListener(ebHandler);
            exitB.setSize(100,30);
            exitB.setLocation(200,30);      
    
            pane.add(label);
    
            pane.add(textField);
            pane.add(startB);
            pane.add(stopB);
            pane.add(exitB);
    
            timer = new Timer(DELAY, new TimerEventHandler());
    
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    
        private class TimerEventHandler implements ActionListener{
            public void actionPerformed(ActionEvent e){
                int r = (int)(9.0 * Math.random()) + 1;
                String str = Integer.toString(r);
                currentNum = "";
                currentNum = str;
                label.setText(str);
                repaint();
            }
        }
    
        public class StartButtonHandler implements ActionListener{
            public void actionPerformed(ActionEvent e){
                timer.start();
            }
        }
    
        public class StopButtonHandler implements ActionListener{
            public void actionPerformed(ActionEvent e){
                timer.stop();
            }
        }
    
        private class ExitButtonHandler implements ActionListener{
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        }
    
        public static void main(String[] args){
            TimerClassPanel timerPanel = new TimerClassPanel();
            JOptionPane.showMessageDialog(null, "Type your guess (int between 1-9)" +
                    " in the field then press 'ENTER'");
        }
    
        @Override
        public void mouseClicked(MouseEvent e) {
            if( e.getX() > 150 && e.getX() < 300 && e.getY() > 0 && e.getY() < 30)
            {   
                textField.setText("");
                repaint();
            }
        }
    
        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void mousePressed(MouseEvent arg0) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub
    
        }
    }