JxBrowser:如何在ContextMenuHandler中实现“另存为”

时间:2019-01-20 12:46:41

标签: jxbrowser

我想在ContextMenuHandler中实现“另存为”选项。

但是没有像loadURL(...)这样的浏览器功能会强制浏览器组件只是为提供的链接开始下载。

1 个答案:

答案 0 :(得分:0)

__________________编辑____________________________

  import com.teamdev.jxbrowser.chromium.Browser;
    import com.teamdev.jxbrowser.chromium.ContextMenuHandler;
    import com.teamdev.jxbrowser.chromium.ContextMenuParams;
    import com.teamdev.jxbrowser.chromium.SavePageType;
    import com.teamdev.jxbrowser.chromium.swing.BrowserView;
    import java.awt.BorderLayout;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JMenuItem;
    import javax.swing.JPopupMenu;
    import javax.swing.SwingUtilities;
    import javax.swing.WindowConstants;


    public class ContextMenuSample {
        public static void main(String[] args) {
            JPopupMenu.setDefaultLightWeightPopupEnabled(false);

            Browser browser = new Browser();
            BrowserView view = new BrowserView(browser);

            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.add(view, BorderLayout.CENTER);
            frame.setSize(700, 500);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            browser.setContextMenuHandler(new MyContextMenuHandler(view));
            browser.loadURL("http://www.google.com");
        }

        private static class MyContextMenuHandler implements ContextMenuHandler {

            private final JComponent component;

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

            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;
            }

            public void showContextMenu(final ContextMenuParams params) {
                final JPopupMenu popupMenu = new JPopupMenu();

                final Browser browser = params.getBrowser();
                popupMenu.add(createMenuItem("Save As", new Runnable() {
                    @Override
                    public void run() {
                        Browser browser2 = new Browser();
                        browser2.loadURL(params.getLinkURL());
                        browser2.saveWebPage(params.getLinkText(), ".", SavePageType.COMPLETE_HTML);
                    }
                }));

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