The Artima Developer Community
Sponsored Link

Java Answers Forum
JPopUpMenu component trouble

2 replies on 1 page. Most recent reply: Apr 24, 2002 2:18 PM by Julio C. Fonseca C.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Julio C. Fonseca C.

Posts: 18
Nickname: jfonseca
Registered: Mar, 2002

JPopUpMenu component trouble Posted: Apr 9, 2002 7:57 AM
Reply to this message Reply
Advertisement
Hello,

I'm using a JPopUpMenu and once it is showed, If I want to show it again without selecting any item, neccesarly I have to select one menu item or at least, click in any other part of the component associated to the JPopUpMenu,

so, it seems, that once a JPopUpMenu is showed, then after two clicks the JPopUp will be showed again, I would like to show the JPopUpMenu everytime is clicked in my panel (I will put the code), without selecting any menu item.
import javax.swing.*;
import javax.swing.event.*;

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

import java.applet.*;
import java.util.*;
import java.net.*;
import java.io.*;


public class MyApplet extends JApplet
{
JPopupMenu popup = new JPopupMenu();
JMenuItem cut = new JMenuItem("cut");
JPanel p = new JPanel();

public void init ()
{
popup.add(cut);

cut.addActionListener(new ManejadorEdicion());
p.addMouseListener(new ManejadorMouseClicks());

getContentPane().add(p, BorderLayout.CENTER);
}

private class ManejadorEdicion implements ActionListener
{
public void actionPerformed( ActionEvent e )
{ public void actionPerformed( ActionEvent e )
{
if (e.getSource() == cut)
System.out.println("Accion cortar ...");
}
}


private class ManejadorMouseClicks extends MouseAdapter
{
public void mousePressed (MouseEvent e)
{
System.out.println("Hello ...!, let's show the popup");
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
}

and finally the .html file,

<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
width ="800" height="500" align="baseline"
codebase = "http://java.sun.com/products/plugin/1.2/jinstall-12-win32.cab#Version=1,2,0,0" >
<PARAM NAME = "code" VALUE = "MyApplet.class">
<PARAM NAME = "type" VALUE = "application/x-java-applet;version=1.2">
</OBJECT>

Thanks in advance,

Julio


Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: JPopUpMenu component trouble Posted: Apr 11, 2002 5:53 PM
Reply to this message Reply
That was an interesting question turns out, all you needed was a couple of methods in your adapter:
	private class ManejadorMouseClicks extends MouseAdapter
	{
		/**
		 *  Original code.
		 *
			public void mousePressed (MouseEvent e)
			{
				System.out.println("Hello ...!, let's show the popup");
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		*/
		
		public void mouseClicked(MouseEvent event)
		{
			if (SwingUtilities.isRightMouseButton(e))
			popup.show((Component)event.getComponent(), event.getX(), event.getY());
		}
 
		public void mouseReleased(MouseEvent event)
		{
		  if(event.isPopupTrigger())
			popup.show((Component)event.getSource(), event.getX(), event.getY());
		}
	}

Julio C. Fonseca C.

Posts: 18
Nickname: jfonseca
Registered: Mar, 2002

Re: JPopUpMenu component trouble Posted: Apr 24, 2002 2:18 PM
Reply to this message Reply
nice solution!

Thanks
J

Flat View: This topic has 2 replies on 1 page
Topic: cannot resolve symbol Previous Topic   Next Topic Topic: Help!  Creating a user-customizable GUI

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use