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");
//Nesting
a MouseListener for the WebRenderer browser
browser.addMouseListener(new
MouseAdapter() { public
void onMouseDown(MouseEvent e) { //Check
if it is a right click if
(SwingUtilities.isRightMouseButton()) { //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); } } });