Solved

Standard way of creating a popup menu on mouse right click

Posted on 2007-12-01
4
236 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now