Here you can find the source of clickButton(final Window window, final String buttonText)
static boolean clickButton(final Window window, final String buttonText)
//package com.java2s; // it under the terms of the GNU General Public License as published by import java.awt.Component; import java.awt.Container; import java.awt.Window; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JButton; public class Main { static final SimpleDateFormat _DateFormatter = new SimpleDateFormat("HH:mm:ss:SSS"); static boolean clickButton(final Window window, final String buttonText) { final JButton button = findButton(window, buttonText); if (button == null) return false; if (!button.isEnabled()) { button.setEnabled(true);//from w w w .ja v a 2s . c o m logToConsole("Button was disabled, has been enabled: " + buttonText); } logToConsole("Click button: " + buttonText); button.doClick(); if (!button.isEnabled()) logToConsole("Button now disabled: " + buttonText); return true; } /** * Traverse a container hierarchy and returns the button with * the given text * */ static JButton findButton(Container container, String text) { Component[] components = container.getComponents(); for (Component component : components) { if (component instanceof JButton) { JButton button = (JButton) component; if (button.getText().equals(text)) { return button; } } else if (component instanceof Container) { JButton button = findButton((Container) component, text); if (button != null) { return button; } } } return null; } /** * writes a text message prefixed with the current time to the console */ static void logToConsole(String msg) { System.out.println(_DateFormatter.format(new Date()) + " IBController: " + msg); } }