Java JButton Settings clickButton(final Window window, final String buttonText)

Here you can find the source of clickButton(final Window window, final String buttonText)

Description

click Button

License

Open Source License

Declaration

static boolean clickButton(final Window window, final String buttonText) 

Method Source Code


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

Related

  1. addToButtonGroup(ButtonGroup bg, T button)
  2. applyDefaultProperties(final Button comp)
  3. askYesNoQuestion(String dlgTitle, String question, Component parent, Object[] buttonTittle)
  4. buttonGroup(AbstractButton b1, AbstractButton b2)
  5. buttonGroup(JToggleButton b1, JToggleButton b2)
  6. configureOKAndCancelButtons(JPanel panel, Action ok, Action cancel)
  7. configureTextAndMnemonic(AbstractButton button, String text)
  8. confirm(Component comp, String title, String message, int buttons, int type)
  9. creaStyledButton(int style)