Swing Mouse Move Events using MouseMotionAdapter

In this tutorial, we will learn how to receive mouse motion events using MouseMotionAdapter.

Swing Mouse Move Events Example

package net.sourcecodeexamples.swingexamples.firstprograms;

import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

/**
 * 
 * @author javaguides.net
 *
 */
public class MouseMoveEventExample extends JFrame {

 private static final long serialVersionUID = 1L;
 private JLabel coords;

    private void initializeUI() {

        coords = new JLabel("");

        createLayout(coords);

        addMouseMotionListener(new MouseMotionAdapter() {

            @Override
            public void mouseMoved(MouseEvent e) {

                super.mouseMoved(e);

                int x = e.getX();
                int y = e.getY();

                String text = String.format("x: %d, y: %d", x, y);

                coords.setText(text);
            }
        });

        setTitle("Mouse move events");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void createLayout(JComponent... arg) {

        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);

        gl.setAutoCreateContainerGaps(true);

        gl.setHorizontalGroup(gl.createParallelGroup()
                .addComponent(arg[0])
                .addGap(250)
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
                .addGap(130)
        );

        pack();
    }


    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
         MouseMoveEventExample moveEventExample = new MouseMoveEventExample();
         moveEventExample.initializeUI();
         moveEventExample.setVisible(true);
        });
    }
}
In the above example, we display the coordinates of a mouse pointer in a label component.
addMouseMotionListener(new MouseMotionAdapter() {

    @Override
    public void mouseMoved(MouseEvent e) {

        super.mouseMoved(e);

        int x = e.getX();
        int y = e.getY();

        var text = String.format("x: %d, y: %d", x, y);

        coords.setText(text);
    }
});
We override the moseMoved() method of the adapter. From the MouseEvent object we get the x and y coordinates of the mouse pointer, build a string and set it to the label.

Output

Related Swing Examples

  • Java Swing Exit Button - In this post, I show you how to exit a Swing application when clicking on the exit button.
  • Java Swing Combo Box Example - In this post, I show you how to create a combo box using a JComboBox component in swing-based applications.
  • Java Swing Slider Example - In this post, I show you how to create a slider using the JSlider component in swing-based applications.

Comments