Home > Net >  how to place mouse directly above a JOptionPane button in java?
how to place mouse directly above a JOptionPane button in java?

Time:10-24

As a button can be preselected to be hit by Enter key , is there an way to place mouse on a selected button of Joption pane Button ?

Thanks in advance

CodePudding user response:

Here is how I did it.
First, create an instance of default dialog

I did some experimenting and discovered that the JOptionPane instance contains two child components.

Component[] cmpts = oPane.getComponents();
for (Component cmpt : cmpts) {
    System.out.println(cmpt.getClass());
}

Each child is a JPanel. One of them contains the OK JButton.

Then I added a WindowListener to the JDialog. When the JDialog is displayed for the first time, I use a Robot to move the mouse pointer to the OK button.

javax.swing.JOptionPane oPane = new javax.swing.JOptionPane();
javax.swing.JDialog dlg = oPane.createDialog("Dialog Title");
dlg.addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowOpened(java.awt.event.WindowEvent wndEvt) {
        javax.swing.JComponent jCmpt = (javax.swing.JComponent) oPane.getComponent(1);
        javax.swing.JButton button = (javax.swing.JButton) jCmpt.getComponent(0);
        java.awt.Point pt = button.getLocationOnScreen();
        try {
            java.awt.Robot robbie = new java.awt.Robot();
            robbie.mouseMove(pt.x, pt.y);
        }
        catch (java.awt.AWTException xAwt) {
            xAwt.printStackTrace();
        }
    }
});
dlg.setVisible(true);

Note that the above is simply a proof of concept (POC). I suggest that you investigate the API of the classes appearing in the above code in order to make it suitable for your requirements.

CodePudding user response:

The following is an mre demonstrating placing the mouse over JOptionPane button:

import java.awt.*;
import javax.swing.*;

public class MouseOverJOptionPaneButton {

    public static void main(String[] args) {

        JOptionPane optionPane = new JOptionPane();
        optionPane.setMessage("Mouse positioned over the button");
        optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
        JButton jButton = new JButton("OK");
        jButton.addActionListener(actionEvent -> {
            System.out.println(jButton.getText()  " pressed");
        });
        optionPane.setOptions(new Object[] { jButton });
        JDialog dialog = optionPane.createDialog("");
        //if modal, placeMouseOver is invoked after dialog closes
        //alternatively use WindowListener as in Arba's answer
        dialog.setModal(false);  
        dialog.setVisible(true);
        placeMouseOver(jButton);
        //todo, optionally:  dialog.setModal(true);
    }

    private static void placeMouseOver(JComponent comp) {
        try {
            //calculate center of component AFTER it is showing
            int xCoord = comp.getLocationOnScreen().x   comp.getWidth()/2;
            int yCoord = comp.getLocationOnScreen().y   comp.getHeight()/2;
            // Move the cursor
            new Robot().mouseMove(xCoord, yCoord);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
}
  • Related