0

I am new to java development, and decided to create a simple java project that outputs the X and Y coordinates of where the mouse is inside of a jframe. I am unsure how I would use a listener.

package main;

import java.awt.BorderLayout;
import java.awt.MouseInfo;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class Main {
public static void main(String[] args) {
jFrame();
}

public static void jFrame() {
    JFrame myFrame = new JFrame("Screen Location");
    myFrame.setVisible(true);
    myFrame.setBounds(0, 0, 300, 100);
    JLabel myText = new JLabel(""+ getTotal(),
        SwingConstants.CENTER);
    myFrame.getContentPane().add(myText, BorderLayout.CENTER);
}

private static String getTotal() {
    for (int i = 0; i < 1;) {
    int publicScreenValueX = MouseInfo.getPointerInfo().getLocation().x;
    int publicScreenValueY = MouseInfo.getPointerInfo().getLocation().y;
    String total = new String(publicScreenValueX + ", " + publicScreenValueY);
    return total;
    }
return null;
  }
}

How would I be able to make the project constantly update the coordinates? Could someone provide a reference on how to do this?

Do note, this is without using a button, or without any input from the user, other than moving the mouse.

Here is an image of what this would look like:

Image of current program

Except the coordinates would always be updating.

conku
  • 79
  • 7

2 Answers2

0

Basically all you have to do in order to create a JFrame that handles mouse events is:

1 - Create a class that extends JFrame and implements MouseListener

2 - @Override mouseClicked, mouseEntered, mouseExited, mousePressed, mouseReleased to monitor the coresponding events. Now every time one of these events occures, the respective function will fire up. Use MouseEvent.getX(), MouseEvent.getY() to get the coordinates of the window that the mouse event occurs.

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;

public class CreateJFrameWindowWithMouseEventHandling extends JFrame implements MouseListener {

    private static final long serialVersionUID = 1L;

    public CreateJFrameWindowWithMouseEventHandling() {
        setTitle("Simple Frame");
        addMouseListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        JOptionPane.showMessageDialog(null, "Mouse Clicked at X: " + String.valueOf(x) + " - Y: " + String.valueOf(y));
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        System.out.println("Mouse Entered frame at X: " + x + " - Y: " + y);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        System.out.println("Mouse Exited frame at X: " + x + " - Y: " + y);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        System.out.println("Mouse Pressed at X: " + x + " - Y: " + y);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        System.out.println("Mouse Released at X: " + x + " - Y: " + y);
    }

    private static void createAndShowGUI() {

  //Create and set up the window.

  JFrame frame = new CreateJFrameWindowWithMouseEventHandling();

  //Display the window.

  frame.setVisible(true);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {

  //Schedule a job for the event-dispatching thread:

  //creating and showing this application's GUI.

  javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

    createAndShowGUI(); 

}

  });
    }

}
Mohsen_Fatemi
  • 2,183
  • 2
  • 16
  • 25
0

I would advise on creating a new class that inherits from JFrame and all that, because it's way more Object Oriented friendly. However, with your code, what you need to do is add a MouseMotionListener to your JFrame, implement all abstract methods and, on the method mouseMoved, change your myText Label's text; Replace the method below with the one you currently have and it should work. Please note that, java.awt.event.MouseMotionListener is only going to give you information about mouse movement inside your application window. For events that occur outside that window, you might want to check this other question: Java mouse motion anywhere on screen

public static void jFrame() {
    JFrame myFrame = new JFrame("Screen Location");
    myFrame.setLocationRelativeTo(null);
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // makes the application close when you close the JFrame
    myFrame.setVisible(true);
    myFrame.setBounds(0, 0, 300, 100);
    JLabel myText = new JLabel(""+ getTotal(), SwingConstants.CENTER);
    myFrame.getContentPane().add(myText, BorderLayout.CENTER);
    myFrame.addMouseMotionListener(new MouseMotionListener(){
        @Override
        public void mouseDragged(MouseEvent e) {
            // don't delete this method
            // all abstract methods must be overridden 
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            myText.setText(getTotal());
        }
    });

}
Community
  • 1
  • 1
Vinicius Victor
  • 348
  • 1
  • 10