MouseMotionListener Help

Need to put one outside the window...

Page 1 of 1

4 Replies - 1684 Views - Last Post: 03 January 2006 - 06:38 PM Rate Topic: -----

#1 CodeFX  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-August 05

MouseMotionListener Help

Posted 02 January 2006 - 09:13 PM

Okay, I'm working on an auto-hiding dock, which I will eventually use to launch my programs from for myself. I'm wondering if it's possible to somehow get my program to listen for the mouse coming near to the far-left side of the screen, while the window is hidden. This would be helpful for bringing the dock out of hiding state. I have searched for hours for any way to do this, and have found... nothing. I have also browsed the entire Toolkit source, looking for anything that might help. And although I might have missed something, I could not see anything that pertains to this problem.

And if there are any other ways to do it, I would welcome the suggestions. I am also open to any other suggestions not pertaining to the problem, as long as they aren't against the forum rules.

This is the current, simple, code for the dock. It does nothing but show the dock and have a close button.

public class Main {
    
    private JWindow gui;
    private JButton exitButton;
    
    /** Creates a new instance of Main */
    public Main() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        int availableHeight = (int)(gc.getBounds().getHeight()) - Toolkit.getDefaultToolkit().getScreenInsets(gc).bottom;
        gui = new JWindow();
        
        exitButton = new JButton("Close");
        exitButton.addActionListener(new CloseListener());
        gui.getContentPane().add(exitButton, BorderLayout.PAGE_END);
        
        gui.setBackground(Color.DARK_GRAY);
        gui.setLocation(0,0);
        gui.setSize(100, availableHeight);
        gui.setVisible(true);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Main prog = new Main();
    }
    
    private class CloseListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
}


Thank you very much for any help anyone can provide.

Is This A Good Question/Topic? 0
  • +

Replies To: MouseMotionListener Help

#2 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: MouseMotionListener Help

Posted 03 January 2006 - 07:30 AM

I'm not sure how you would go about listening for events on the desktop without using JNI, but you can get the mouse position by using the MouseInfo class.
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: MouseMotionListener Help

Posted 03 January 2006 - 08:38 AM

Sounds like you'd have to set up a polling loop...every certain amount of time, check where the mouse is...it's not perfect, but if the interval is small enough, you should be able to get a good approximation.
Was This Post Helpful? 0
  • +
  • -

#4 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: MouseMotionListener Help

Posted 03 January 2006 - 10:38 AM

Amadeus, on 3 Jan, 2006 - 09:35 AM, said:

Sounds like you'd have to set up a polling loop...every certain amount of time, check where the mouse is...it's not perfect, but if the interval is small enough, you should be able to get a good approximation.

You know, I thought about that, but I wasn't sure if it would be good practice. But now that I think about it, I'm not even sure how the underlying Java event system is implemented. For all I know it could be a bunch of polling loops running in their own threads. :)
Was This Post Helpful? 0
  • +
  • -

#5 CodeFX  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-August 05

Re: MouseMotionListener Help

Posted 03 January 2006 - 06:38 PM

Alright, thanks guys. I'll try out the polling loop idea and let you know how it goes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1