Java JFrame handle mouse event
import java.awt.BorderLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JLabel; public class Main extends JFrame { private String details; private final JLabel statusBar = new JLabel("Click the mouse"); public Main() { super("Mouse Clicks and Buttons"); add(statusBar, BorderLayout.SOUTH); addMouseListener(new MouseClickHandler()); // add handler }// ww w . j a va2 s .c o m // inner class to handle mouse events private class MouseClickHandler extends MouseAdapter { // handle mouse-click event and determine which button was pressed @Override public void mouseClicked(MouseEvent event) { int xPos = event.getX(); // get x-position of mouse int yPos = event.getY(); // get y-position of mouse details += "xPos:"+xPos+" yPos:"+ yPos; details = String.format("Clicked %d time(s)", event.getClickCount()); if (event.isMetaDown()) // right mouse button details += " with right mouse button"; else if (event.isAltDown()) // middle mouse button details += " with center mouse button"; else // left mouse button details += " with left mouse button"; statusBar.setText(details); } } public static void main(String[] args) { Main Main = new Main(); Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Main.setSize(400, 150); Main.setVisible(true); } }