text field mouse event java

Solutions on MaxInterview for text field mouse event java by the best coders in the world

showing results for - "text field mouse event java"
Anae
11 Jan 2021
1import java.awt.FlowLayout;
2import java.awt.event.MouseAdapter;
3import java.awt.event.MouseEvent;
4import javax.swing.JFrame;
5import javax.swing.JTextField;
6import javax.swing.SwingUtilities;
7
8public class ClickAndClearDemo {
9    private static void createAndShowGUI(){
10        JFrame frame = new JFrame();
11        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
12        frame.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
13
14        final JTextField textField = new JTextField("Enter text here...");
15        textField.addMouseListener(new MouseAdapter(){
16            @Override
17            public void mouseClicked(MouseEvent e){
18                textField.setText("");
19            }
20        });
21
22        frame.add(textField);
23        frame.pack();
24        frame.setVisible(true);
25    }
26
27    public static void main(String[] args) {
28        SwingUtilities.invokeLater(new Runnable(){
29            @Override
30            public void run() {
31                createAndShowGUI();
32            }
33        });
34    }
35}
36