Solved

Standard way of creating a popup menu on mouse right click

Posted on 2007-12-01
4
255 Views
Last Modified: 2011-09-20
Hi,

What's the accepted/standard way to create a popup menu in java when the user right clicks your applet? I'd prefer something very lightwieght, I just need the bare minimum,

Thanks
0
Comment
Question by:DJ_AM_Juicebox
  • 2
4 Comments
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20388596
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20388597
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20388683
Yeah im not using swing, its just a plain old double buffered drawing applet:

import java.awt.*;

public abstract class BufferedApplet extends java.applet.Applet implements Runnable
{
0
 
LVL 7

Accepted Solution

by:
UrosVidojevic earned 500 total points
ID: 20388690
import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;

    public class PopupMenuTest extends Applet implements ActionListener {

      PopupMenu popup;

      public void init() {         
            MenuItem mi;

          popup = new PopupMenu("Edit");

            mi = new MenuItem("Cut");
            mi.addActionListener(this);
          popup.add(mi);

            mi = new MenuItem("Copy");
            mi.addActionListener(this);
          popup.add(mi);

          popup.addSeparator();

            mi = new MenuItem("Paste");
            mi.addActionListener(this);
          popup.add(mi);

          add(popup); // add popup menu to applet
           
            enableEvents(AWTEvent.MOUSE_EVENT_MASK);

          resize(200, 200);
        }

      public void processMouseEvent(MouseEvent e) {

          if (e.isPopupTrigger()) {
              popup.show(e.getComponent(), e.getX(), e.getY());
          }
          super.processMouseEvent(e);
        }

        public void actionPerformed(ActionEvent e) {
          String command = e.getActionCommand();

            if (command.equals("Cut")) {
              // perform cut operation
            } else if (command.equals("Copy")) {
                // perform copy operation
            } else if (command.equals("Paste")) {
                // perform paste operation
            }
        }
    }
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question