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 createyour 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 keymasks 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 subclassone 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.pack();    __dialog.addWindowListener(new WindowAdapter() {      public void windowClosing(WindowEvent e) {	__dialog.setVisible(false);      }    }    );    __button.addMouseListener(new MouseAdapter() {      public void mouseClicked(MouseEvent e) {	if((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0)	  __dialog.setVisible(true);      }    }    );    add(__button);  }  public void dispose() {    __dialog.dispose();    super.dispose();  }	  public static void main(String[] args) {    Frame frame;    WindowListener exitListener;    exitListener = new WindowAdapter() {      public void windowClosing(WindowEvent e) {	Window window = e.getWindow();	window.setVisible(false);	window.dispose();	System.exit(0);      }    };    frame = new RightClick();    frame.addWindowListener(exitListener);    frame.pack();    frame.setVisible(true);  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a