Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)


Since 6.0 version JxBrowser supports two rendering modes: heavyweight (default mode) and lightweight. Drag and drop functionality is supported in both heavyweight and lightweight rendering modes, but for each rendering mode it works differently.(从6.0版开始,JxBrowser支持两种渲染模式:重量级(默认模式)和轻量级。重量级和轻量级渲染模式均支持拖放功能,但是每种渲染模式的工作方式都不同。)


Lightweight Rendering Mode(轻量级渲染模式)

In lightweight rendering mode Drag and Drop functionality is implemented using Java Swing and JavaFX Drag&Drop API. Drag and Drop support is limited. It supports only the following features:(在轻量级渲染模式下,拖放功能是使用Java Swing和JavaFX拖放API实现的。拖放支持受到限制。它仅支持以下功能:)

  • Drag selected text and links on loaded web page and drop them into other applications.(将选定的文本和链接拖放到加载的网页上,然后将其拖放到其他应用程序中。)
  • Drag selected text and links on loaded web page and drop them into text fields or text areas on the same web page.(将选定的文本和链接拖放到已加载的网页上,然后将其拖放到同一网页上的文本字段或文本区域中。)
  • Drag text from other applications and drop it into text fields or text areas on loaded web page.(从其他应用程序中拖动文本,然后将其拖放到已加载网页上的文本字段或文本区域中。)


Heavyweight Rendering Mode(重量级渲染模式)

In heavyweight rendering mode Drag and Drop functionality is implemented by Chromium engine. Chromium handles all Drag and Drop operations, so it works exactly as in Google Chromium.(在重量级渲染模式下,拖放功能由Chromium引擎实现。 Chromium可处理所有拖放操作,因此其工作原理与Google Chromium中的完全相同。)


Disabling Drag and Drop(禁用拖放)

By default Drag and Drop is enabled. To disable Drag and Drop use the BrowserView.setDragAndDropEnabled(boolean enabled) method or the jxbrowser.chromium.dnd.enabled=false System Property.(默认情况下启用拖放。要禁用拖放,请使用BrowserView.setDragAndDropEnabled(启用布尔值)方法或jxbrowser.chromium.dnd.enabled = false系统属性。)


Swing JxBrowser 6.3 and higher(Swing JxBrowser 6.3及更高版本)


import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;

/**
 * The sample demonstrates how to disable functionality that allows
 * dragging/dropping content from/onto the loaded web page.
 * <p/>
 * By default Drag&Drop is enabled. You can disable default behavior
 * using the "jxbrowser.chromium.dnd.enabled" System Property. For example:
 * System.setProperty("jxbrowser.chromium.dnd.enabled", "false");
 */
public class DisableDragAndDropSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

        // Disable Drag and Drop.
        view.setDragAndDropEnabled(false);

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

        browser.loadURL("http://www.google.com");
    }
}

JavaFX JxBrowser 6.3 and higher(JavaFX JxBrowser 6.3及更高版本)


import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserCore;
import com.teamdev.jxbrowser.chromium.BrowserType;
import com.teamdev.jxbrowser.chromium.internal.Environment;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
import com.teamdev.jxbrowser.chromium.javafx.internal.LightWeightWidget;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

/**
 * The sample demonstrates how to disable functionality that allows dragging
 * links from the loaded web page.
 */
public class JavaFXDisableDnDSample extends Application {

    @Override
    public void init() throws Exception {
        // On Mac OS X Chromium engine must be initialized in non-UI thread.
        if (Environment.isMac()) {
            BrowserCore.initialize();
        }
    }

    @Override
    public void start(final Stage primaryStage) {
        Browser browser = new Browser(BrowserType.LIGHTWEIGHT);
        final BrowserView view = new BrowserView(browser);

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                LightWeightWidget lightWeightWidget = (LightWeightWidget)
                        view.getChildren().get(0);
                if (lightWeightWidget.isDragAndDropEnabled()) {
                    // Now you cannot drag and drop links from the loaded web page.
                    lightWeightWidget.setDragAndDropEnabled(false);
                }
            }
        });

        Scene scene = new Scene(new BorderPane(view), 700, 500);
        primaryStage.setScene(scene);
        primaryStage.show();

        browser.loadURL("http://www.google.com");
    }

    public static void main(String[] args) {
        launch(args);
    }
}