dcsimg
setComponentPopupMenu on a JPanel does not by itself display a popup menu.
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mike_Hanafey
Posted On:   Monday, April 18, 2005 12:51 PM

JDK 1.5 adds the setComponentPopupMenu method to JComponent , but if this method is used to add a popup menu to a JPanel , a right mouse click does nothing. Adding a do nothing MouseListener to the panel makes the popup come alive. This behaviour seems counter intuitive. Is this a bug, or is there a another more logical way to indicate that the panel should handle popup events? Using the setInheritsPopupMenu on a JComponent added to the panel does make that component display the inherited menu. In the example that follows, only panelA shows the popup, because it is the one with the added empty MouseListener . /* =======   More>>

JDK 1.5 adds the setComponentPopupMenu method to JComponent , but if this method is used to add a popup menu to a JPanel , a right mouse click does nothing. Adding a do nothing MouseListener to the panel makes the popup come alive. This behaviour seems counter intuitive.


Is this a bug, or is there a another more logical way to indicate that the panel should handle popup events?


Using the setInheritsPopupMenu on a JComponent added to the panel does make that component display the inherited menu.


In the example that follows, only panelA shows the popup, because it is the one with the added empty MouseListener .

			
/* ========================================================================
* $Id$
* Michael K. Hanafey
* Apr 18, 2005 2:04:35 PM
* ------------------------------------------------------------------------ */

package swing.popup;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


/**
* EgA
* @author Michael K. Hanafey
* @version 0.1
*/

public class EgA extends JFrame implements MouseListener {

private JPanel panelA = new JPanel();
private JPanel panelB = new JPanel();
private JPopupMenu popup = new JPopupMenu();
private JMenuItem itemA = new JMenuItem("Item A");
private JMenuItem itemB = new JMenuItem("Item B");
private JButton buttonA = new JButton("A");
private JButton buttonB = new JButton("B");

public EgA() {
buttonA.setInheritsPopupMenu(true);
buttonB.setInheritsPopupMenu(true);
panelA.setBorder(BorderFactory.createEtchedBorder());
panelB.setBorder(BorderFactory.createEtchedBorder());
panelA.addMouseListener(this);
panelA.add(buttonA);
panelB.add(buttonB);
add(panelA, BorderLayout.CENTER);
add(panelB, BorderLayout.NORTH);
popup.add(itemA);
popup.add(itemB);
panelA.setComponentPopupMenu(popup);
panelB.setComponentPopupMenu(popup);
}

public static void main(String[] args) {
final EgA a = new EgA();
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setSize(500, 500);
a.validate();
a.setVisible(true);
}

public void mouseClicked(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}
}

   <<Less
About | Sitemap | Contact