public void mouseClicked(MouseEvent e) { if(e.getButton() == MouseEvent.BUTTON1) { System.out.println("Left mouse button clicked"); } }
public void mousePressed(MouseEvent e) { if(e.getButton() == MouseEvent.BUTTON3) { // do something when right mouse button is pressed } }This example shows how to use the getButton() method to check if the right mouse button was pressed when the mousePressed event is triggered. The method performs some action when the right mouse button is pressed. Both of these examples are part of the java.awt.event package library.