• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to simulate MouseEvent without using mouse for java program?

 
Mandar Khire
Ranch Hand
Posts: 572
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just curious about this question!
I wrote very simple program which has MouseEvent as follows:-


If i run this Example program, & run another simulator program written in java(or any other programming language)(in windows or Linux),
so by simulator program how can i give mouseClicked Event to Example program?

In java source code i try to find out how Event works then i found following:-
in Event.java which in package java.awt

Even as per EventDispatchThread.java there is if-else as follows:-

In package java.awt.event i found MouseEvent.java
But now confuse how to use all those things for prepare simulator?
Need help & experts guidance!
 
Campbell Ritchie
Marshal
Posts: 52585
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't understand the second part of your post. What do you mean about the if statement?
 
Piet Souris
Rancher
Posts: 1641
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Mandar,

neither do I understand the second part of your opening post.

But for an external program to do a click on your button, a possibility is:

make that button a private member of every Example instance. Call it,
say, myButton.
Add a method to your Example class, something like

Your external program can then simply do:

You might want to put a flag in this method, that allows this click or not,
so that you can programmatically switch off this external clicking.

By the way: usually you use an ActionListener on a JButton.

Hope this helps.

Greetz,
Piet
 
Mandar Khire
Ranch Hand
Posts: 572
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell Ritchie & Piet Souris,
Sorry for inconvenience due to me.


I can't understand the second part of your post. What do you mean about the if statement?

As per my thoughts When program use Events then in background java create Thread who always ready to capture event, when ever it get particular event then it will validate & give appropriate values to program so we can see event occurs.

Regarding to mouse event capturing i found c++ code which as below:-

So with the help of this code might be i will get clue.

Piet Souris , your idea also nice & i will work on it.
My main aim is not to simulate with 'doClick()' but with 'mouseClicked(MouseEvent arg0)'

I run this cpp program after running java program, before running this cpp program i kept mouse on button(but not press key).
Due to this cpp program button automatically get clicked & text shown in text-field.
This all happened with help of cpp program but question is can we write program in java same as it?
Still i studying on this topic. Hope i will get solution.
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mandar Khire wrote:
If i run this Example program, & run another simulator program written in java(or any other programming language)(in windows or Linux),
so by simulator program how can i give mouseClicked Event to Example program?


I'm not sure I understand everything you are asking, but let me mention a couple of things:

1) A single mouseClicked event is not enough to trigger a JButton. One must move the mouse pointer over the button, press the mouse button (which moves the JButton into the armed state), and then (without moving the cursor too far away) release the mouse button. So you will have to do all of those things.

2) You should probably check out Robot class, which is located in the java.awt package. It provides methods that can simulate moving, pressing, and releasing the mouse.
 
Mandar Khire
Ranch Hand
Posts: 572
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Brian Cole,
I'm not sure I understand everything you are asking,

If anyone has any question for clarify the question which i asked,please ask here.So it more helpful.
A single mouseClicked event is not enough to trigger a JButton.One must move the mouse pointer over the button, press the mouse button (which moves the JButton into the armed state), and then (without moving the cursor too far away) release the mouse button. So you will have to do all of those things.

In C++ program i am trying to give fix location of JButton where mouse should be click, & if mouse pointer not on proper location then due to this program it should come on JButton automatically.
With help of JNI, i am thinking to make program which simulate MouseEvent because as per my thoughts when we try to capture events which actually handle by Drivers or API which helps to handle it. & Those Drivers & API is in C or C++.
Now in my C++ code we can see
X11/Xlib.h
This is the main header file for Xlib. The majority of all Xlib symbols are declared by including this file. This file also contains the preprocessor symbol XlibSpecificationRelease. This symbol is defined to have the 6 in this release of the standard. (Release 5 of Xlib was the first release to have this symbol.)

X11/Xutil.h
This file declares various functions, types, and symbols used for inter-client communication and application utility functions, which are described in "Inter-Client Communication Functions" and "Application Utility Functions". X11/Xlib.h must be included before including this file.

I dont know how to directly call API its members/functions/variables in Java directly. But i am trying to learn how to use JNI.


2) You should probably check out Robot class, which is located in the java.awt package. It provides methods that can simulate moving, pressing, and releasing the mouse.

I will study this, might it will help me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!