Soft click on a GUI button in Java Swing

How would I programmatically click on a Swing JButton in such a way as to log all the corresponding action / mouse events and be visible to the user (i.e. they will click the button as if they actually clicked)?

The button is in the same application in which I am running; I am not trying to control a button in another application. I assume that I could directly inject events into the queue, but I would prefer to avoid this approach, if possible, nor will it show a visible click.

I see that the java.awt.Robot class offers methods for moving the mouse and clicking the mouse, but not for clicking on a specific button.

+44
java swing awtrobot
Feb 24 '11 at 19:09
source share
6 answers

Have you tried using doClick () ?

+85
Feb 24 '11 at 19:12
source share

If doClick() not what you want, you can move the mouse to the button and click it:

 public void click(AbstractButton button, int millis) throws AWTException { Point p = button.getLocationOnScreen(); Robot r = new Robot(); r.mouseMove(px + button.getWidth() / 2, py + button.getHeight() / 2); r.mousePress(InputEvent.BUTTON1_MASK); try { Thread.sleep(millis); } catch (Exception e) {} r.mouseRelease(InputEvent.BUTTON1_MASK); } 
+9
Feb 24 '11 at 19:31
source share

You can always simulate it by activating an event event with it as a source.

http://download.oracle.com/javase/6/docs/api/java/awt/event/ActionEvent.html

To fire it, create the action event above and any listener you just want to call

 ActionEvent e = new ActionEvent(myButton,1234,"CommandToPeform"); myListener.actionPerformed(e); 
+2
Feb 24 '11 at 19:12
source share

Despite the fact that the button.doClick() was satisfied with button.doClick() , I was looking for something like what happens after installing the mnemonics, i.e. using button.setMnemonic(KeyEvent.VK_A) . In fact, you can hold ALT + A without any action (other than a visual change). And after releasing key A (with or without ALT), the button starts an ActionEvent.

I found that I can get ButtonModel (see Java 8 API ) using button.getModel() , then visually click the button using model.setPressed(true); model.setArmed(true); model.setPressed(true); model.setArmed(true); (both changed by mnemonics) and visually release the button by setting both values ​​to false . And when model.setPressed(false) is called, when the button is pressed and armed, the button automatically launches an ActionEvent (calling model.setArmed(false) only visually changes the button).

[Quote from the Java API documentation ButtonModel] A button is launched, and an ActionEvent is activated when the mouse is released while arming the model [...]

To make the application respond to keystrokes when the button is visible (without the contents of the window or the button that should be the focus owner, that is, when another component in the window is focused), I used key bindings (see the Official Java Guide ).

Work code: Press SHIFT + A to visually press the button (as opposed to pressing ALT with the key after the mnemonic is set using button.setMnemonic() ). And release the key to print the action command ("button") on the console.

 // MnemonicCode.java import javax.swing.*; import java.awt.event.*; public class MnemonicCode extends JFrame { public MnemonicCode(int keyCode) { JButton button = new JButton("button"); getContentPane().add(button); addMnemonicToButton(button,keyCode); button.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { System.out.println(e.getActionCommand()); } }); pack(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) throws Exception { MnemonicCode bp = new MnemonicCode(KeyEvent.VK_A); } void addMnemonicToButton(JButton button,int keyCode) { int shiftMask = InputEvent.SHIFT_DOWN_MASK; // signature: getKeyStroke(int keyCode, int modifiers, boolean onKeyRelease) KeyStroke keyPress = KeyStroke.getKeyStroke(keyCode,shiftMask,false); KeyStroke keyReleaseWithShift = KeyStroke.getKeyStroke(keyCode,shiftMask,true); // get maps for key bindings InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); ActionMap actionMap = button.getActionMap(); // add key bindings for pressing and releasing the button inputMap.put(keyPress,"press"+keyCode); actionMap.put("press"+keyCode, new ButtonPress(button)); inputMap.put(keyReleaseWithShift,"releaseWithShift"+keyCode); actionMap.put("releaseWithShift"+keyCode, new ButtonRelease(button)); ///* // add key binding for releasing SHIFT before A // if you use more than one modifier it gets really messy KeyStroke keyReleaseAfterShift = KeyStroke.getKeyStroke(keyCode,0,true); inputMap.put(keyReleaseAfterShift,"releaseAfterShift"+keyCode); actionMap.put("releaseAfterShift"+keyCode, new ButtonRelease(button)); //*/ } class ButtonPress extends AbstractAction { private JButton button; private ButtonModel model; ButtonPress(JButton button) { this.button = button; this.model = button.getModel(); } public void actionPerformed(ActionEvent e) { // visually press the button model.setPressed(true); model.setArmed(true); button.requestFocusInWindow(); } } class ButtonRelease extends AbstractAction { private ButtonModel model; ButtonRelease(JButton button) { this.model = button.getModel(); } public void actionPerformed(ActionEvent e) { if (model.isPressed()) { // visually release the button // setPressed(false) also makes the button fire an ActionEvent model.setPressed(false); model.setArmed(false); } } } } 
+2
May 23 '14 at 15:17
source share

From: http://download.oracle.com/javase/6/docs/api/javax/swing/JButton.html

 /** * Click a button on screen * * @param button Button to click * @param millis Time that button will remain "clicked" in milliseconds */ public void click(AbstractButton button, int millis) { b.doClick(millis); } 
+1
Feb 24 '11 at 19:14
source share

Based on @Courteaux's answer, this method clicks the first cell in JTable:

 private void clickFirstCell() { try { jTable1.changeSelection(0, 0, false, false); Point p = jTable1.getLocationOnScreen(); Rectangle cellRect = jTable1.getCellRect(0, 0, true); Robot r = new Robot(); Point mouse = MouseInfo.getPointerInfo().getLocation(); r.mouseMove(px + cellRect.x + cellRect.width / 2, py + cellRect.y + cellRect.height / 2); r.mousePress(InputEvent.BUTTON1_MASK); try { Thread.sleep(50); } catch (Exception e) { } r.mouseRelease(InputEvent.BUTTON1_MASK); r.mouseMove(mouse.x, mouse.y); } catch (AWTException ex) { } } 
0
Jun 15 '16 at 7:36
source share



All Articles