Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes Listening to the Mouse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Listening to the Mouse" Watch "Listening to the Mouse" New topic
Author

Listening to the Mouse

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
If I have a little application running that does not use Swing, how can I get it to read what my mouse is doing? For example :
if I run a class that simply displays the number 1 using System.out.println(), it will write it to the DOS window. Now, say if I move my mouse while the DOS window is still in view and I want the X co-ordinate of the mouse to be displayed in the DOS window, what do I do?
So far, I've written my simple display class which implements the MouseListener. I add the methods such as mousePressed(), passing in the MouseEvent class (called mocls). Now I would like to do System.out.println(mocls.getX()). However this obviously won't work because I'm not adding the MouseListener to any object. I've only done this before with AWT or Swing components where you simply addMouseListener(this) but that's because addMouseListener is 'inheritable' from the Component or Container class.
After all that my question is basically; how do I add a listener to an object that is not Swing or AWT?
Thanks for any help....
Paul
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Paul,
You are basically going to have to use Swing or AWT... or write an entire GUI library for your own use... This is because, without Swing or the AWT components, there is nothing to listen for events or to create events... Plus, any text that goes to System.out is static... you can't really affect it( i.e. make it an event listener or event creator ).
HTH,
-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
Thanks for the response. That's waht I needed to know. Now another question: if I write my own component to inherit a Swing component, what do I do if I'm already extending a clas that I need for my little class. Is there an interface I can implement to give me all the listener capabilities? - or would I have to write my own interface that extends JComponent (or something) and then implement that?
Thanks for you help.
Paul
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Paul,
I am not sure exactly what you are asking...
If you are extending one of the JComponent classes ( like JButton, etc. ) all you have to do to add a listener to it is to call addxListener in the code that uses it. For example, to add a MouseListener to a component you have created called JWhatever, that extends a JComponent that already listens for MouseEvents you would use it like this :

where someMouseListener can be this if the class containing this code implements MouseListener, or it could be a seperate class that implements MouseListener, or it could be an anonymous inner class of MouseListener.
However, if the component you are creating does not inherit from a class that listens for MouseEvents, you will have to do a few things...

    [list]Add the following line to any constructors of your new class :


    [/list][list]Add a method to your class :


    [/list]
    These steps add internal processing of MouseEvents of your new component... MouseListeners can still be added to your new component so mouse actions can be detected, but your component will always process mouse actions internally too... This is how buttons always do their little up/down drawing when they are pushed... they do internal processing.
    I hope that I have understood the question you were asking and have helped you out,
    -Nate
 
 
subject: Listening to the Mouse
 
Similar Threads
How do I capture a mouseClicked event from a JTable within a JScrollPane
Correct this code for event handling.
How to add own Listener if add method is not supported?
Do MouseEvents work on JComboBox
basic question about events