Posted By:
Bahman_Barzideh
Posted On:
Wednesday, January 29, 2003 07:59 AM
Technically it is possible to simulate a mouse event. But I am not sure
it is a particularly good idea (more on that later). You can do what you
are trying to do by
abusing the
processMouseEvent ()in
Container. Here is some psuedo-code:
public class BadIdea extends JButton {
public BadIdea () {
// some code
}
public void simulateMouseClick () {
MouseEvent event = new MouseEvent (this, MouseEvent.MOUSE_CLICKED,
System.currentTimeMillis (), 0, 0, 0, 1, false);
// the first 2 params would be as here, you figure out the rest
processMouseEvent (event);
}
}
calling
simulateMouseClick should simulate a
MOUSE_CLICK, a similar function could be
developed to simulate a
MOUSE_PRESSED, etc.
Having said all this, I don't think you should write such code. My
primary argument is that I believe
processMouseEvent () is
a
protected method as a side-effect not by design (the JFC/AWT
designers has no choice but to make this routine
protected).
If this is so, the routine can change/disappear without notice in a future
release.
A further reason is that mouse events are quite complex. You usually
don't just have a
MOUSE_CLICK event. There would be a
MOUSE_PRESS, a
MOUSE_RELEASE, and a
MOUSE_CLICK for a single click (not to mention mouse motion events).
My suggestion would be to design your app so that you can use a more
robust mechanism. For example, you mention "hyperlink clicks". You
may want to look into The
JTextPane class. It has a rich
set of features for supporting text and has built-in support for
hyperlinks.
Hope this is of some help.