Swing Mouse Move Events using MouseMotionAdapter

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

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

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare