Article View: borland.public.jbuilder.java.api
Article #2584Re: setting a mouse position
From: John P. McGrath
Date: Tue, 19 Sep 2000 00:00
Date: Tue, 19 Sep 2000 00:00
72 lines
2826 bytes
2826 bytes
On Mon, 18 Sep 2000 10:58:51 -0400, Frank wrote:
> I am using Windows NT and I got the AWTException on compilation.
> From the documentation I read that on some platforms this will
> not work. Is this my case?
I cannot tell what the problem is from that description. Was there a
message with the exception? Was the exception from the Robot constructor
or from the mouseMove() call or somewhere else. Did you get a traceback?
Here is a program I wrote to test the feature. Try running it and see if
it works for you.
====================================================================
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class RobotTest
extends JFrame
{
private JPanel buttonPanel = new JPanel();
private JButton moveButton = new JButton();
private JButton exitButton = new JButton();
private Robot robot;
////////////////////////////////////////////////////////////////////
static public void main( String[] args ) {
try {
JFrame frame = new RobotTest();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.show();
} catch ( Exception ex ) {
ex.printStackTrace();
}
}
public RobotTest()
throws AWTException
{
moveButton.setText( "Move mouse" );
moveButton.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent event ) {
Point pos = exitButton.getLocationOnScreen();
Dimension size = exitButton.getSize();
robot.mouseMove( pos.x + size.width / 2,
pos.y + size.height / 2 );
}
} );
buttonPanel.add( moveButton, null );
exitButton.setText("Exit");
exitButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit( 0 );
}
} );
buttonPanel.add( exitButton, null );
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
robot = new Robot();
}
}
====================================================================
--
Regards,
John McGrath
Technical Details
Message-ID:
<MPG.1430d42637fca1c3989b49@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<39c24c25$1_2@dnews> <MPG.142ce891cc0c9c54989b3a@forums.borland.com> <39c629e8$1_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,95a818d94a09e7b1 X-Google-Attributes: gid1089d0,public From: John P. McGrath <JPMcGrath@fast.net> Subject: Re: setting a mouse position Date: 2000/09/19 Message-ID: <MPG.1430d42637fca1c3989b49@forums.borland.com> X-Deja-AN: 671528630 References: <39c24c25$1_2@dnews> <MPG.142ce891cc0c9c54989b3a@forums.borland.com> <39c629e8$1_2@dnews> Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.api