0

So i have this code, however i dont get it how to set the mouse coordinates to the label every time the mouse moves ...

timer.schedule(new TimerTask() { @Override public void run() { int mouseX = MouseInfo.getPointerInfo().getLocation().x; int mouseY = MouseInfo.getPointerInfo().getLocation().y; lblInfo.setText("Nada "+mouseX+mouseY); } }, 1); 

Im not even sure if the code is right but what i want it to do is to get the mouse coordinates in the label called lblInfo every time the mouse moves.

This code what does is only display it once whenever the program starts...

2
  • 2
    See How to write a MouseMotionListener Commented Feb 16, 2014 at 12:45
  • How often is the run() method executed? Add a println() to see. Commented Feb 16, 2014 at 13:07

2 Answers 2

2

You need to implements MouseMotionListener, then add your logic inside mouseMoved method like:

public class MyClass implements MouseMotionListener { public void mouseMoved(MouseEvent e) { System.out.println("X : " + e.getX()); System.out.println("Y : " + e.getY()); } public void mouseDragged(MouseEvent e) { //do something } } 

Read more about MouseMotionListener

Sign up to request clarification or add additional context in comments.

4 Comments

Thats the problem, i have extends MouseAdapter which inherits MouseMotionListener (if i remember correctly) , but how do i tell it to set coordinates every time the mouse moves. I can get the coordinates every time the mouse is pressed inside a JPanel for example, but not everywhere on screen. I am using mouseClicked and it works fine... But cant manage to find the right event to make it update the coordinates every time the mouse moves, and mouse Moved doesnt seem to work omg...
public void mouseMoved(MouseEvent e){ int mouseX = e.getX(); int mouseY = e.getY(); System.out.println("Test Info: "+mouseX+" "+mouseY); igu.lblInfo.setText("Test Info: "+mouseX+" "+mouseY); }
docs.oracle.com/javase/7/docs/api/java/awt/event/… ,mouseMoved does not do this "Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed." and i dont know why...
doh nvm im dumb xD in stead of adding addMouseMotionListener(); i used addMouseListener(); Thanks for the help :D
0

Have a look at this example. You first need to implement mousePresseded then mouseDragged. The first to get the point of the initial press, then the mouseDragged will use those coordinates.

addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { // Get x,y and store them pX = me.getX(); pY = me.getY(); } }); addMouseMotionListener(new MouseAdapter() { public void mouseDragged(MouseEvent me) { frame.setLocation(frame.getLocation().x + me.getX() - pX, frame.getLocation().y + me.getY() - pY); } }); 

Complete running example. It uses an undecorate frame and create a JPanel as the header, that you can drag to move the frame.

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.LineBorder; public class UndecoratedExample { static JFrame frame = new JFrame(); static class MainPanel extends JPanel { public MainPanel() { setBackground(Color.gray); } @Override public Dimension getPreferredSize() { return new Dimension(400, 400); } } static class BorderPanel extends JPanel { JLabel stackLabel; int pX, pY; public BorderPanel() { ImageIcon icon = new ImageIcon(getClass().getResource( "/resources/stackoverflow1.png")); stackLabel = new JLabel(); stackLabel.setIcon(icon); setBackground(Color.black); setLayout(new FlowLayout(FlowLayout.RIGHT)); add(stackLabel); stackLabel.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { System.exit(0); } }); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { // Get x,y and store them pX = me.getX(); pY = me.getY(); } }); addMouseMotionListener(new MouseAdapter() { public void mouseDragged(MouseEvent me) { frame.setLocation(frame.getLocation().x + me.getX() - pX, frame.getLocation().y + me.getY() - pY); } }); } } static class OutsidePanel extends JPanel { public OutsidePanel() { setLayout(new BorderLayout()); add(new MainPanel(), BorderLayout.CENTER); add(new BorderPanel(), BorderLayout.PAGE_START); setBorder(new LineBorder(Color.BLACK, 5)); } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { frame.setUndecorated(true); frame.add(new OutsidePanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.