Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: AWT
Expertise: Beginner
Oct 2, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Mouse Event Handling

How do I program the right mouse button to bring up a window or dialog box when I right-click on a particular button?

All Java AWT components exhibit certain default behaviors with respect to mouse events. It is possible to enhance these behaviors by creating your own mouse event handlers. More generally, it is possible to customize the behavior of any component with respect to any event by creating your own event handlers. You can even create your own custom events. Event handlers are created by implementing the java.awt.EventListener interface, and then specifying a set of methods that follow a protocol defined by the programmer or already defined by the Java core API.

To handle mouse events, you would implement the java.awt.MouseListener interface, which defines a set of methods that have a special meaning to the AWT when handling mouse events. For example, when a mouse button is pressed inside a component, all the registered MouseListeners of a component have their mousePressed(MouseEvent event) method invoked. The MouseEvent parameter contains extra information about the event, including which mouse button was pressed. MouseEvent is a sublcass of InputEvent, which defines a set of modifier masks of an event, including mouse button masks. The right button corresponds to InputEvent.BUTTON3_MASK. You can test whether or not a mouse button press, click, or release was caused by the right mouse button by logically anding the result of MouseEvent.getModifiers() with InputEvent.BUTTON3_MASK. If the result is non-zero, the right button was involved. You can similarily test for the other button and key masks defined InputEvent.

When you want to test only for a particular event and don't need to implement all the methods defined in an event listener, you can subclass one of the adapter classes, such as MouseAdapter, provided in the java.awt.event package. Each of the adapter classes implements all of the methods of a given event listener interface with empty methods. This allows you to override just one or two of the methods, without having to define every single one. The following example shows how you can create a MouseAdapter that will respond to a right mouse click by showing a dialog box.

import java.awt.*;
import java.awt.event.*;

public final class RightClick extends Frame {
  private Button __button;
  private Dialog __dialog;

  public RightClick() {
    super("Right Click Demo");
    __button = new Button("Right click");
    __dialog = new Dialog(this);
    __dialog.add(new Label("Dialog box"));
    __dialog.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
    __button.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
	if((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0)

  public void dispose() {

  public static void main(String[] args) {
    Frame frame;
    WindowListener exitListener;

    exitListener = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
	Window window = e.getWindow();

    frame = new RightClick();
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date