Home » Java » java – JXbrowser custom context menu showing only in window's right edge-Exceptionshub

java – JXbrowser custom context menu showing only in window's right edge-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am using JxBrowser 6.23.1 with the following code

public AppWindow(){

    setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
    setSize(1200,800);
    setLocationRelativeTo(null);
    setVisible(true);
    toFront();

    mBrowser = new Browser();
    BrowserView browserView = new BrowserView(mBrowser);

    mBrowser.setContextMenuHandler(new MyContextMenuHandler(browserView));
    mBrowser.loadURL("www.google.com");

    add(browserView, BorderLayout.CENTER);
}

private static class MyContextMenuHandler implements ContextMenuHandler {

    private final JComponent component;

    private MyContextMenuHandler(JComponent parentComponent) {
        this.component = parentComponent;
    }

    public void showContextMenu(final ContextMenuParams params) {
        final JPopupMenu popupMenu = new JPopupMenu();
        if (!params.getPageURL().isEmpty()) {
            popupMenu.add(createMenuItem("Copy Link", new Runnable() {
                public void run() {
                    String linkURL = params.getPageURL();
                    StringSelection selection = new StringSelection(linkURL);
                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                    clipboard.setContents(selection, selection);
                }
            }));

            popupMenu.add(createMenuItem("Open link in new window", new Runnable() {
                public void run() {
                    String linkURL = params.getPageURL();
                    System.out.println("linkURL = " + linkURL);
                }
            }));
        }

        final Browser browser = params.getBrowser();
        popupMenu.add(createMenuItem("Reload", new Runnable() {
            public void run() {
                browser.reload();
            }
        }));

        final Point location = params.getLocation();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                popupMenu.show(component, location.x, location.y);
            }
        });
    }

    private static JMenuItem createMenuItem(String title, final Runnable action) {
        JMenuItem reloadMenuItem = new JMenuItem(title);
        reloadMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                action.run();
            }
        });
        return reloadMenuItem;
    }
}

As my app is, the context menu when I right-click is being shown only on the right edge of my window, like the last 200px from the right edge. If I click anywhere in the window closer to the center, it doesn’t show anything although the popupMenu.show(component, location.x, location.y); is invoked in my debug.

Is it a bug in this JxBrowser’s version or something that I have missed?

How to&Answers: