Alex Rangerisng Alex Rangerisng - 4 years ago 123
Java Question

When I run my autoclicker I can't stop it

If I click on start button I can't click back or stop. I need to stop the app. One way to stop the app is that you must power off computer :D. When the app is running, start button is locked and I can't click on it. Click on the exit button doesn't work. This app is simple AutoClicker. Exist in java something better to do autoclicker than robot class? How can I fix it? Is there error anywhere? Or is there something I can do to stop the app?

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Robot;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JCheckBox;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.Color;

public class AutoClicker extends JFrame {

private JPanel contentPane;
private final JLabel m = new JLabel("AutoClicker");

* Launch the application.
public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AutoClicker frame = new AutoClicker();
} catch (Exception e) {

* Create the frame.
public AutoClicker() {

setBounds(100, 100, 147, 162);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

JCheckBox chckbxOnTop = new JCheckBox("On Top");
boolean onTop = false;
chckbxOnTop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {


chckbxOnTop.setBounds(6, 7, 97, 23);

JCheckBox chckbxAutoclicker = new JCheckBox("AutoClicker");
chckbxAutoclicker.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

try {
Robot r = new Robot();
} catch (AWTException e1) {



chckbxAutoclicker.setBounds(6, 80, 97, 23);
m.setForeground(new Color(153, 102, 0));
m.setBounds(16, 92, 120, 31);

Answer Source

Swing is single threaded - calling any long running task on that thread will lock that thread up (the EDT) and prevent any painting, events, etc... from occurring. One of the ActionListener implementations creates an infinite loop:


The above will never evaluate to false, because it is evaluating on the EDT, and events (such as disabling the JCheckBox to allow this method to return false) cannot occur until the EDT is free. If you wish to continually run a task while allowing the EDT to performs its necessary tasks, you have three options:

  1. Create a new Thread. Note any calls to Swing from this thread should be dispatched to the EDT using SwingUtilities.invoke*
  2. Use a SwingWorker
  3. If you wish to do something at a later time on the EDT, or run something on a schedule on the EDT, use a javax.swing.Timer
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download