• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Listening to the Mouse

 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic