> i tried that but it doesn't seem to work. ...
Here is a little example I made.
Does it work as you expect?import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** Example of using the Robot to type in a text field,
use the enter key, and activate a button via the space bar. */
class ActivateButtonUsingRobot {
static Robot robot;
static void activateKey(int keycode) {
robot.keyPress( keycode );
robot.keyRelease( keycode );
robot.delay( 150 );
}
public static void main(String[] args)
throws AWTException {
// if headless, exit early
robot = new Robot();
final JFrame f = new JFrame();
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
JTextArea ta = new JTextArea("1st line!",10,20);
f.getContentPane().add(ta, BorderLayout.CENTER);
JButton button = new JButton("Press Me!");
button.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae ) {
JOptionPane.showMessageDialog(
f, "Button Activated!" );
}
} );
f.getContentPane().add(button, BorderLayout.SOUTH);
f.pack();
// put frame in center of screen
f.setLocationRelativeTo(null);
f.setVisible(true);
Dimension screensize =
Toolkit.getDefaultToolkit().getScreenSize();
// move mouse to center of screen, and ..
robot.mouseMove(
screensize.width/2, screensize.height/2 );
// ..activate the frame
robot.mousePress( InputEvent.BUTTON1_MASK );
robot.mouseRelease( InputEvent.BUTTON1_MASK );
robot.delay( 400 );
// try using enter key..
activateKey( KeyEvent.VK_ENTER );
activateKey( KeyEvent.VK_ENTER );
// this text should have a blank line above it..
activateKey( KeyEvent.VK_H );
activateKey( KeyEvent.VK_I );
// move to the button
robot.keyPress( KeyEvent.VK_CONTROL );
robot.keyPress( KeyEvent.VK_TAB );
robot.keyRelease( KeyEvent.VK_TAB );
robot.keyRelease( KeyEvent.VK_CONTROL );
robot.delay( 500 );
// try activating the button..
activateKey( KeyEvent.VK_SPACE );
}
}