Java JMenus are particularly useful in creating default context menus
for right-click activation.
We
have constructed a small example
application demonstrating the
use of JMenus and WebRenderer.
When the menu items are clicked
a message will be output to
the command line from each menu
item.
The core pieces
of the code are as follows:
IBrowserCanvas
browser = BrowserFactory.spawnMozilla();
/*
* Disabling the default context menu right-click
menu) */ browser.enableDefaultContextMenu(false);
JPopupMenu menu
= new JPopupMenu("Context Menu"); JMenuItem
menuItem1 = new JMenuItem("Menu Item
1"); JMenuItem
menuItem2 = new JMenuItem("Menu Item
2"); JMenuItem
menuItem3 = new JMenuItem("Menu Item
3");
/*
* Making the menu heavyweight - this prevents
the menu going behind
* the canvas. This is a bug with Swing menus,other
Swing components do
* not have this issue.
*/ menu.setLightWeightPopupEnabled(false);
//Nesting
a MouseListener for the WebRenderer browser
browser.addMouseListener(new
MouseAdapter() { public
void onMouseDown(MouseEvent e) { //Check
if it is a right click if
(e.getModifiers() == 4) { //If
it is a right click then show the menu
int x =
e.getX(); int y =
e.getY(); menu.show(browser.getCanvas(),
x, y); menu.repaint(); }
else { menu.setVisible(false); } } });
Core
code
Making the PopupMenu a heavyweight component is essential
to ensure that the component shows above
WebRenderer. setLightWeightPopupEnabled(false)
sets the popup menu to heavyweight and has
the correct Z-Order.