Home > Enterprise >  How to automatically click 'YES' in JOptionPane in Java?
How to automatically click 'YES' in JOptionPane in Java?

Time:12-28

I am using Cucumber to test an open-source Java Swing application. I have tried many ways to click 'Yes' on a JOptionPane that pops up (like setting ch = 0 and ch = JOptionPane.YES_OPTION), and it does not work. Here is the code:

public JButton btnAdd;
public int ch;

btnAdd = new JButton("Add");
        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String name=textField.getText();
                double bal=Double.parseDouble(textField_1.getText());
                double maxw=Double.parseDouble(textField_2.getText());
                if(bal<2000) {
                    JOptionPane.showMessageDialog(getComponent(0), "Minimum Limit 5000", "Warning", 0);
                    textField.setText(null);
                    textField_1.setText(null);
                    textField_2.setText(null);
                }
                else {
                    if(name==null||bal<=0||maxw<=0) {
                        JOptionPane.showMessageDialog(getComponent(0),"Typing Mismatch!! Try Again");
                        textField.setText(null);
                        textField_1.setText(null);
                        textField_2.setText(null);
                    }
                    else {
                        ch=JOptionPane.showConfirmDialog(getComponent(0), "Confirm?");
                        if(ch==0) {
                            int index = FileIO.bank.addAccount(name, bal, maxw);
                            DisplayList.arr.addElement(FileIO.bank.getAccounts()[index].toString());
                            JOptionPane.showMessageDialog(getComponent(0),"Added Successfully");
                            dispose();
                        }
                        else {
                            JOptionPane.showMessageDialog(getComponent(0),"Failed");
                            textField.setText(null);
                            textField_1.setText(null);
                            textField_2.setText(null);
                        }
                        textField.setText(null);
                        textField_1.setText(null);
                        textField_2.setText(null);
                    }
                }
            }

Is there a way I can click 'Yes'? Thanks!

CodePudding user response:

Here is an example, how to press "Yes" button in all option dialogs, that appears

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.AWTEventListener;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.function.Predicate;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class PressOptionButton {

    private final AWTEventListener windowListener = this::pressYes;
    
    public static void main(String[] args) {
        Locale.setDefault(Locale.ENGLISH); // make names of buttons in JOptionPane English
        SwingUtilities.invokeLater(new PressOptionButton()::initUI);
    }
    
    private void initUI() {
        Toolkit.getDefaultToolkit().addAWTEventListener(windowListener, AWTEvent.WINDOW_EVENT_MASK);
        JOptionPane.showConfirmDialog(null, "Here is a simple confirm dialog", "Confirm!", JOptionPane.YES_NO_CANCEL_OPTION);
        JOptionPane.showConfirmDialog(null, "Here is another confirm dialog", "Confirm!", JOptionPane.YES_NO_OPTION);
        JPanel p = new JPanel(new BorderLayout(10, 10));
        p.add(new JLabel("Enter something"), BorderLayout.WEST);
        p.add(new JTextField(10));
        JOptionPane.showConfirmDialog(null, p, "Confirm!", JOptionPane.YES_NO_CANCEL_OPTION);
        // don't forget to remove the listener when no longer required.
        Toolkit.getDefaultToolkit().removeAWTEventListener(windowListener);
    }
    
    private void pressYes(AWTEvent e) {
        if (e instanceof WindowEvent && ((WindowEvent) e).getID() == WindowEvent.WINDOW_ACTIVATED) {
            Window w = ((WindowEvent) e).getWindow();
            List<JOptionPane> pane = getAllChildrenOfClass(w, JOptionPane.class);
            if (pane.size() == 1) {
                JOptionPane root = pane.get(0);
                for (JButton b : getAllChildrenOfClass(root, JButton.class)) {
                    if ("Yes".equals(b.getText())) {
                        b.doClick(2000); // if you want no delay, use 0 as parameter
                        break;
                    }
                }
            }
        }
    }
    
    /**
     * Searches for all children of the given component which are instances of the given class.
     *
     * @param aRoot start object for search. May not be null.
     * @param aClass class to search. May not be null.
     * @param <E> class of component.
     * @return list of all children of the given component which are instances of the given class. Never null.
     */
    public static <E> List<E> getAllChildrenOfClass(Container aRoot, Class<E> aClass) {
        return getAllChildrenOfClass(aRoot, aClass, e -> true);
    }

    /**
     * Searches for all children of the given component which are instances of the given class and satisfies the given condition.
     *
     * @param aRoot start object for search. May not be null.
     * @param aClass class to search. May not be null.
     * @param condition condition to be satisfied. May not be null.
     * @param <E> class of component.
     * @return list of all children of the given component which are instances of the given class. Never null.
     */
    public static <E> List<E> getAllChildrenOfClass(Container aRoot, Class<E> aClass, Predicate<E> condition) {
        final List<E> result = new ArrayList<>();
        final Component[] children = aRoot.getComponents();
        for (final Component c : children) {
            if (aClass.isInstance(c) && condition.test(aClass.cast(c))) {
                result.add(aClass.cast(c));
            }
            if (c instanceof Container) {
                result.addAll(getAllChildrenOfClass((Container) c, aClass, condition));
            }
        }
        return result;
    }
}
  • Related