Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8082679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:12:00+00:00 2026-06-05T17:12:00+00:00

I’m using java.awt.Robot for integration tests of my Swing application, but I’m having trouble

  • 0

I’m using java.awt.Robot for integration tests of my Swing application, but I’m having trouble running my actions in the correct order. How can I tell the thread that calls robot.mousePressed(...) to block until Swing is finished dispatching that event? Apparently, robot.setAutoWaitForIdle(true) does no good.

Here’s my demo. I expect the “robot finished!” message to always come after “Action finished blocking.”, but instead it often happens too soon instead.

import java.awt.AWTException;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.sql.Date;
import java.text.DateFormat;
import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;


public class RobotWaitForIdleDemo {
    /**
     * Create the device that contains the given point in screen coordinates.
     * Robot has to be constructed differently for each monitor.
     */
    public static GraphicsDevice getDevice(Point p) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gs = ge.getScreenDevices();

        // Search the devices for the one that draws the specified point.
        for (GraphicsDevice device : gs) {
            GraphicsConfiguration configuration = device.getDefaultConfiguration();
            Rectangle bounds = configuration.getBounds();
            if (bounds.contains(p)) {
                return device;
            }
        }
        return null;
    }
    public static final Logger logger = Logger.getLogger(RobotWaitForIdleDemo.class.getName());
    public static void main(String[] args) {
        LogManager.getLogManager().reset();
        Formatter formatter = new Formatter() {
            @Override
            public String format(LogRecord arg0) {
                Date date = new Date(arg0.getMillis());
                DateFormat.getTimeInstance().format(date);
                return String.format("%s %s %s %s%n",
                        DateFormat.getTimeInstance().format(date),
                        arg0.getLoggerName(),
                        arg0.getLevel(),
                        arg0.getMessage());
            }
        };
        ConsoleHandler consoleHandler = new ConsoleHandler();
        consoleHandler.setFormatter(formatter);
        logger.addHandler(consoleHandler);

        final JFrame jframe = new JFrame("Robot experiment");
        GroupLayout groupLayout = new GroupLayout(jframe.getContentPane());

        final JButton jbutton = new JButton("Click me!");
        jbutton.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                // Simulate a heavy Swing event handler.
                logger.info("(swing thread) Action starting to block...");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e1) {}
                logger.info("(swing thread) Action finished blocking.");
            }
        });

        JButton tryAgainBUtton = new JButton("Automatically click above button.");
        tryAgainBUtton.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                new Thread(new Runnable() {
                    @Override public void run() {
                        try {
                            Point point = new Point(jbutton.getWidth()/2,jbutton.getHeight()/2);
                            SwingUtilities.convertPointToScreen(point, jbutton);
                            GraphicsDevice device = getDevice(point);
                            Point offset = device.getDefaultConfiguration().getBounds().getLocation();

                            Robot robot = new Robot(device);
                            robot.setAutoWaitForIdle(true);
                            robot.setAutoDelay(30);

                            robot.mouseMove(point.x - offset.x, point.y - offset.y);
                            String threadName = Thread.currentThread().getName();
                            logger.info(String.format("(%s) robot.mousePress(%d)", threadName, InputEvent.BUTTON1_MASK));
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            logger.info(String.format("(%s) robot.mouseRelease(%d)", threadName, InputEvent.BUTTON1_MASK));
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            logger.info(String.format("(%s) robot finished!", threadName, InputEvent.BUTTON1_MASK));
                        } catch (AWTException ex) {
                            ex.printStackTrace();
                        }
                    }
                }, "robot thread").start();
            }
        });

        jframe.getContentPane().setLayout(groupLayout);
        groupLayout.setAutoCreateGaps(true);
        groupLayout.setAutoCreateContainerGaps(true);
        groupLayout.setVerticalGroup(
                groupLayout.createSequentialGroup()
                    .addComponent(jbutton)
                    .addComponent(tryAgainBUtton));
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup()
                    .addComponent(jbutton)
                    .addComponent(tryAgainBUtton)                           );

        jframe.setSize(300, 300);
        jframe.setVisible(true);
        jframe.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
}

I’m running Java 1.6 on Ubuntu.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:12:03+00:00Added an answer on June 5, 2026 at 5:12 pm

    maybe this one can help you, notice not tested in Java7

    you can test that in each of steps for isEventDispatchThread()

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import sun.awt.SunToolkit;
    
    public class TestMenu {
    
        /**
         * Without a delay, SunToolkit may encounter a problem in SunToolkit (at
         * least in JDK 6, where the drop down size problem is not present).
         * 
         * Note: SunToolkit also has some mechanism to delay, but I forgot how it
         * worked.
         * 
         * <pre>
         * Exception in thread "main" sun.awt.SunToolkit$InfiniteLoop
         *         at sun.awt.SunToolkit.realSync(Unknown Source)
         *         at TestMenu.syncAndDelay(TestMenu.java:172)
         *         at TestMenu.click(TestMenu.java:88)
         *         at TestMenu.moveAndClickCenter(TestMenu.java:150)
         *         at TestMenu.main(TestMenu.java:45)
         * </pre>
         * 
         * As a bonus, the delay makes the scenario better visible for the human
         * eye.
         */
        private static int delay = 500;
        private static JMenu[] menus = new JMenu[5];
        private static Dimension[] parentSizes;
        private static Robot robot;
        private static SunToolkit toolkit;
    
        public static void main(String[] args) throws Exception {
            robot = new Robot();
            toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
            parentSizes = new Dimension[menus.length];
            createGUI(); // Open the first menu. Then get the drop down size of all menu's
            moveAndClickCenter(menus[0]);
            for (int index = 0; index < menus.length; index++) {
                parentSizes[index] = getDropDownSize(index);
            }// Click the last item on the last menu.        
            Component item = menus[menus.length - 1].getMenuComponent(menus[menus.length - 1].getMenuComponentCount() - 1);
            moveAndClickCenter(item);
            // Open the last drop down again. Then get the drop down sizes once more. If size not equal to previous size, then it's a bug.
            boolean bug = false;
            moveAndClickCenter(menus[menus.length - 1]);
            for (int index = menus.length - 1; index >= 0; index--) {
                Dimension currentSize = getDropDownSize(index);
                System.out.print("old: " + parentSizes[index] + ", new: " + currentSize);
                if (!parentSizes[index].equals(currentSize)) {
                    bug = true;
                    System.out.println(" ERROR");
                } else {
                    System.out.println();
                }
            }
            if (bug) {
                throw new RuntimeException("JMenu drop down size is changed for no reason.");
            }
    
        }
    
        private static Dimension getDropDownSize(int index) throws Exception {
            moveToCenter(menus[index]);
            return menus[index].getMenuComponent(0).getParent().getSize();
        }
    
        private static void click() throws Exception {
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            syncAndDelay();
        }
    
        private static void createGUI() throws Exception {
    
            SwingUtilities.invokeAndWait(new Runnable() {
    
                @Override
                public void run() {
                    UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();// The L&F defines the drop down policy.
                    for (final UIManager.LookAndFeelInfo info : infos) {
                        if (info.getName().toLowerCase().indexOf("metal") >= 0) {
                            if (!UIManager.getLookAndFeel().getName().equals(info.getName())) {
                                try {
                                    UIManager.setLookAndFeel(info.getClassName());
                                    System.out.println("Attempt to set look and feel to " + info.getName());
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            } else {
                                System.out.println("Metal look and feel is the default");
                            }
                            break;
                        }
                    }
                    System.out.println("Testing with " + UIManager.getLookAndFeel().getName());  // Setup the GUI.
                    JFrame frame = new JFrame("A frame");
                    frame.setJMenuBar(new JMenuBar());
                    for (int menuIndex = 0; menuIndex < menus.length; menuIndex++) {
                        menus[menuIndex] = new JMenu("Menu " + menuIndex);
                        frame.getJMenuBar().add(menus[menuIndex]);
                        for (int itemIndex = 0; itemIndex <= menus.length - menuIndex; itemIndex++) {
                            // It seems that the problem only occurs if the drop down is displayed outside the frame at the right
                            // (not sure though). A rather long item name.
                            JMenuItem item = new JMenuItem("Menu " + menuIndex + " item " + itemIndex);
                            menus[menuIndex].add(item);
                        }
                    }
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
            syncAndDelay();
        }
    
        private static void moveAndClickCenter(Component c) throws Exception {
            moveToCenter(c);
            click();
        }
    
        private static void moveToCenter(final Component c) throws Exception {
            final Point cp = new Point();
            SwingUtilities.invokeAndWait(new Runnable() {
    
                @Override
                public void run() {
                    Point p = new Point(c.getWidth() / 2, c.getHeight() / 2);
                    SwingUtilities.convertPointToScreen(p, c);
                    cp.setLocation(p);
                }
            });
            robot.mouseMove(cp.x, cp.y);
            syncAndDelay();
        }
    
        private static void syncAndDelay() throws Exception {
            if (delay > 0) {
                Thread.sleep(delay);
            }
            toolkit.realSync();
        }
    
        private TestMenu() {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.