Add a context menu on ContentArea

Hello !

I try to add a popup menu on the CContentArea but this seems to work only on minimize areas and not on the center of the area. How add a popup menu on the CContentArea ?

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import bibliothek.gui.dock.common.CContentArea;
import bibliothek.gui.dock.common.CControl;

public class ContextMenu {

    public ContextMenu() {
        final JPopupMenu contextMenu = new JPopupMenu("Edit");
        contextMenu.add(makeMenuItem("Save"));
        contextMenu.add(makeMenuItem("Save As"));
        contextMenu.add(makeMenuItem("Close"));

        JFrame frame = new JFrame();

        final CControl control = new CControl(frame);
        CContentArea contentArea = control.getContentArea();
        frame.add(contentArea);

        JPanel panel = contentArea;//new JPanel();
        frame.add(panel);
        panel.setComponentPopupMenu(contextMenu);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 200);
        frame.setVisible(true);
    }

    private JMenuItem makeMenuItem(String label) {
        JMenuItem item = new JMenuItem(label);
        return item;
    }

    public static void main(String[] args) {
        new ContextMenu();
    }
}


Thanks.

Gilles

As there is a MouseListener on the center-panel the MouseEvents seem not to fall through (but then I did never design the CContentArea to support context menus). You can however add the menu directly to the center:
contentArea.getCenter().getContentPane().setComponentPopupMenu( contextMenu );

I was not sure that the center-panel can support contextual menus but I’m glad to see that the framework is so flexible.
Thanks Beni.

It was not designed to support context menus. If it does, it is more a coincidence than a plan…