import com.teamdev.jxbrowser.Browser; import com.teamdev.jxbrowser.BrowserFactory; import com.teamdev.jxbrowser.BrowserType; import com.teamdev.jxbrowser.ContextMenuHandler; import com.teamdev.jxbrowser.events.ContextMenuEvent; import javax.swing.*; import java.awt.*; /** * This sample demonstrates how to customize browser's context menu. */ public class ContextMenuSample { public static void main(String[] args) { JPopupMenu.setDefaultLightWeightPopupEnabled(false); final JMenuItem linkItem = new JMenuItem("Link Action"); final JPopupMenu popupMenu = new JPopupMenu("Sample Popup Menu"); popupMenu.add(new JMenuItem("Simple Action")); Browser browser = BrowserFactory.createBrowser(BrowserType.Mozilla15); browser.setContextMenuHandler(new ContextMenuHandler() { public void showContextMenu(final ContextMenuEvent event) { if ("A".equals(event.getTarget().getNodeName())) { popupMenu.add(linkItem); } else { popupMenu.remove(linkItem); } final Point location = event.getLocation(); SwingUtilities.invokeLater(new Runnable() { public void run() { popupMenu.show(event.getBrowser().getComponent(), location.x, location.y); } }); } }); browser.navigate("http://www.google.com"); JMenu fileMenu = new JMenu("File"); fileMenu.add(new JMenuItem("Open...")); fileMenu.add(new JMenuItem("Close")); JMenu helpMenu = new JMenu("Help"); helpMenu.add(new JMenuItem("About...")); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(browser.getComponent(), BorderLayout.CENTER); frame.setSize(640, 480); frame.setLocationRelativeTo(null); frame.setVisible(true); } } |
Examples >