FileDocCategorySizeDatePackage
RightClickGlassPane.javaAPI DocExample4016Mon Jan 09 11:01:58 GMT 2006None

RightClickGlassPane

public class RightClickGlassPane extends JComponent implements MouseListener, MouseMotionListener

Fields Summary
protected JPopupMenu
popup
protected Container
contentPane
Constructors Summary
public RightClickGlassPane(Container contentPane, JPopupMenu menu)

        addMouseListener(this);
        addMouseMotionListener(this);
        this.contentPane = contentPane;
        popup = menu;
    
Methods Summary
protected voiddoDispatch(java.awt.event.MouseEvent e)

        // since it's not a popup we need to redispatch it.
    
        Component component = getRealComponent(e.getPoint());
    
        // return if nothing was found
        if (component == null) {
            return;
        }
    
        // convert point relative to the target component
        Point componentPoint = SwingUtilities.convertPoint(
            this,
            e.getPoint(),
            component);
    
        // redispatch the event
        component.dispatchEvent(new MouseEvent(component,
            e.getID(),
            e.getWhen(),
            e.getModifiers(),
            componentPoint.x,
            componentPoint.y,
            e.getClickCount(),
            e.isPopupTrigger()));
    
protected java.awt.ComponentgetRealComponent(java.awt.Point pt)

        // get the mouse click point relative to the content pane
        Point containerPoint = 
            SwingUtilities.convertPoint(this, pt,contentPane);
    
        // find the component that under this point
        Component component = SwingUtilities.getDeepestComponentAt(
                    contentPane,
                    containerPoint.x,
                    containerPoint.y);
        return component;
    
public static voidmain(java.lang.String[] args)

        // create a frame with some components in it
        JFrame frame = new JFrame("Right Click Test");
        JButton button = new JButton("this is a button");
        JTextField tf = new JTextField("this is a textfield");
        JPanel panel = new JPanel();
        panel.add(button);
        panel.add(tf);
        frame.getContentPane().add(panel);


        JPopupMenu popup = new JPopupMenu();
        popup.add(new JMenuItem("Dogs"));
        popup.add(new JMenuItem("Cats"));
        popup.add(new JMenuItem("Mass Hysteria"));

        // create the right click glass pane.
        Component rc = new RightClickGlassPane(frame.getContentPane(),popup);
        // set as glasspane and make it visible
        frame.setGlassPane(rc);
        rc.setVisible(true);

        // pack and show the frame
        frame.pack();
        frame.setSize(400,200);
        frame.show();
    
public voidmouseClicked(java.awt.event.MouseEvent e)

        redispatchMouseEvent(e, false);
    
public voidmouseDragged(java.awt.event.MouseEvent e)

        redispatchMouseEvent(e, false);
    
public voidmouseEntered(java.awt.event.MouseEvent e)

        redispatchMouseEvent(e, false);
    
public voidmouseExited(java.awt.event.MouseEvent e)

        redispatchMouseEvent(e, false);
    
public voidmouseMoved(java.awt.event.MouseEvent e)

        redispatchMouseEvent(e, false);
    
public voidmousePressed(java.awt.event.MouseEvent e)

        redispatchMouseEvent(e, false);
    
public voidmouseReleased(java.awt.event.MouseEvent e)

        redispatchMouseEvent(e, false);
    
public static voidp(java.lang.String str)

        System.out.println(str);
    
public voidpaint(java.awt.Graphics g)

        //g.drawString("I'm a glass pane",50,50);
    
protected voidredispatchMouseEvent(java.awt.event.MouseEvent e, boolean repaint)


        // if it's a popup
        if(e.isPopupTrigger()) {
            // show the popup and return
            showPopup(e.getComponent(), e.getX(), e.getY());
        } else {
            doDispatch(e);
        }
    
public voidshowPopup(java.awt.Component comp, int x, int y)

        popup.show(comp,x,y);