> 默認是不顯示右鍵菜單的,需要注冊Browser.setContextMenuHandler。如果是重量級模式下,需要設置一下 JPopupMenu.setDefaultLightWeightPopupEnabled(false);,否則顯示不 出來輕量級的上下文菜單
>
默認情況下,當用戶右鍵單擊加載的網頁時,JxBrowser不會顯示上下文菜單。不支持標準Chromium上下文菜單,因為它是Google Chromium應用程序的一部分。
要在用戶右鍵單擊加載的網頁時顯示上下文菜單,您必須注冊ContextMenuHandler實現。當用戶右鍵單擊網頁時,將調用ContextMenuHandler.showContextMenu(ContextMenuParams params)方法。此方法接收ContextMenuParams參數,該參數包含有關網頁上鼠標右鍵單擊位置的HTML元素的信息,鼠標指針的位置,鼠標指針下的HTML元素類型,鏈接URL,鏈接文本,圖像src屬性值等。使用此信息配置上下文菜單。
要注冊ContextMenuHandler的實現,請使用Browser.setContextMenuHandler(ContextMenuHandler contextMenuHandler)方法。
### Swing
```
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.ContextMenuHandler;
import com.teamdev.jxbrowser.chromium.ContextMenuParams;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* The sample demonstrates how to register custom ContextMenuHandler,
* to handle mouse right clicks and display custom Swing context menu.
*/
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;
}
public void showContextMenu(final ContextMenuParams params) {
final JPopupMenu popupMenu = new JPopupMenu();
if (!params.getLinkText().isEmpty()) {
popupMenu.add(createMenuItem("Open link in new window", new Runnable() {
public void run() {
String linkURL = params.getLinkURL();
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;
}
}
}
```
### JavaFX
```
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.ContextMenuHandler;
import com.teamdev.jxbrowser.chromium.ContextMenuParams;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.*;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.awt.*;
/**
* The sample demonstrates how to register custom ContextMenuHandler,
* to handle mouse right clicks and display custom JavaFX context menu.
*/
public class JavaFXContextMenuSample extends Application {
@Override
public void start(Stage primaryStage) {
Browser browser = new Browser();
BrowserView browserView = new BrowserView(browser);
browser.setContextMenuHandler(new MyContextMenuHandler(browserView));
StackPane pane = new StackPane();
pane.getChildren().add(browserView);
Scene scene = new Scene(pane, 700, 500);
primaryStage.setScene(scene);
primaryStage.show();
browser.loadURL("http://www.google.com");
}
public static void main(String[] args) {
launch(args);
}
private static class MyContextMenuHandler implements ContextMenuHandler {
private final Pane pane;
private MyContextMenuHandler(Pane paren) {
this.pane = paren;
}
public void showContextMenu(final ContextMenuParams params) {
Platform.runLater(new Runnable() {
@Override
public void run() {
createAndDisplayContextMenu(params);
}
});
}
private void createAndDisplayContextMenu(final ContextMenuParams params) {
final ContextMenu contextMenu = new ContextMenu();
// Since context menu doesn't auto hide, listen mouse press events
// on BrowserView and hide context menu on mouse press
pane.getChildren().get(0).setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
contextMenu.hide();
}
});
// If there's link under mouse pointer, create and add
// the "Open link in new window" menu item to our context menu
if (!params.getLinkText().isEmpty()) {
contextMenu.getItems().add(createMenuItem(
"Open link in new window", new Runnable() {
public void run() {
String linkURL = params.getLinkURL();
System.out.println("linkURL = " + linkURL);
}
}));
}
// Create and add "Reload" menu item to our context menu
contextMenu.getItems().add(createMenuItem("Reload", new Runnable() {
public void run() {
params.getBrowser().reload();
}
}));
// Display context menu at required location on screen
Point location = params.getLocation();
Point2D screenLocation = pane.localToScreen(location.x, location.y);
contextMenu.show(pane, screenLocation.getX(), screenLocation.getY());
}
private static MenuItem createMenuItem(String title, final Runnable action) {
MenuItem menuItem = new MenuItem(title);
menuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
action.run();
}
});
return menuItem;
}
}
}
```
- 前言
- CSS
- VUE
- Vue.js 安裝
- Vue.js 目錄結構
- Vue.js 起步
- Vue.js 模板語法
- Vue.js 條件與循環
- Vue.js 循環語句
- Vue.js 計算屬性
- Vue.js 監聽屬性
- Vue.js 樣式綁定
- Vue.js 事件處理器
- Vue.js 表單
- Vue.js 組件
- Vue.js 自定義指令
- Vue.js 路由
- React
- 安裝
- React JSX
- React 組件
- 問題1
- React state
- React Props
- React 組件 API
- React 組件生命周期
- React AJAX
- React 表單與事件
- React Refs
- Babel
- Ant Design
- 安裝
- 快速上手
- webpack
- 安裝
- JavaScript
- 知識點
- 字符轉數字
- js中字符串全部替換
- 函數
- reduce() 方法
- UI控件
- DataTable
- 語言配置 選項
- 增加行
- 列渲染-自定義列
- 創建行回調-操作行
- 自定義數據長度
- 默認設置
- 樣式
- 集成Bootstrap 3
- 分頁相關
- 數據
- NodeJs
- Electron
- 打包
- 介紹
- 知識點
- 使用 jquery
- CommonJS規范
- Bower
- 簡介
- 安裝
- Swing
- Swing界面組件
- JComboBox
- JDesktopPane和JInternalFrame
- JFrame
- JTabbedPane
- JTable
- JProgressBar
- JToolBar
- 知識點
- 截取log4j日志并輸出到GUI組件
- JFrame 居中顯示
- Swing中三種最大化初始窗口的方法
- Layout布局
- BorderLayout
- GridBagLayout
- GridLayout
- BoxLayout
- JxBrowser
- 瀏覽器引擎-Browser Engine
- 創建瀏覽器-Creating Browser
- 創建隱身瀏覽器-Creating Incognito Browser
- 存儲用戶數據-Storing User Data
- 處理瀏覽器-Disposing Browser
- 瀏覽器偏好-Browser Preferences
- 恢復瀏覽器-Restoring Browser
- 渲染流程事件-Render Process Events
- 渲染進程ID-Render Process ID
- 獲取幀ID-Getting Frame IDs
- 獲取產品版本-Getting Product Version
- 尋找文本-Finding Text
- 清除緩存-Clearing Cache
- 轉發鍵盤事件-Forwarding Key Events
- 轉發鼠標事件-Forwarding Mouse Events
- 加載內容-Loading Content
- 加載網址-Loading URL
- 使用POST加載URL-Loading URL with POST
- 加載HTML-Loading HTML
- 從JAR加載HTML-Loading HTML from JAR
- 獲取HTML-Getting HTML
- 獲取選定的HTML-Getting Selected HTML
- 加載事件-Loading Events
- 正在加載和等待-Loading & Waiting
- 顯示PDF-Displaying PDF
- 網絡活動-Network Events
- 處理資源加載-Handling Resources Loading
- 啟用/禁用退格導航-Enabling/Disabling Backspace Navigation
- 處理SSL證書錯誤-Handling SSL Certificate Errors
- SSL證書驗證程序-SSL Certificate Verifier
- 導航歷史-Navigation History
- User-Agent
- WebSockets
- 處理加載-Handling Loading
- 修改POST / PUT / PATCH上傳數據-Modifying POST/PUT/PATCH Upload Data
- HTML5本地和會話存儲-HTML5 Local & Session storages
- 訪問HTTP響應數據-Accessing HTTP response data
- HTTP服務器白名單-HTTP Server Whitelist
- 自定義協議處理程序-Custom Protocol Handler
- ActiveX
- 瀏覽器視圖-Browser View
- 輕量級或重量級-Lightweight or Heavyweight
- 在Swing中使用JxBrowser-Using JxBrowser in Swing
- 在JavaFX中使用JxBrowser-Using JxBrowser in JavaFX
- 在SWT中使用JxBrowser-Using JxBrowser in SWT
- 自定義CSS光標-Custom CSS Cursors
- 標題事件-Title Events
- 狀態事件-Status Events
- 鍵盤和鼠標事件-Keyboard & Mouse Events
- 處理鍵盤事件-Handling Keyboard Events
- 處理鼠標事件-Handling Mouse Events
- 編輯器命令-Editor Commands
- 拖放-Drag & Drop
- 內容縮放-Content scaling
- 上下文菜單-Context Menu
- JMenuBar
- JInternalFrame
- JTabbedPane
- JPanel
- 加速輕量級渲染-Accelerated Lightweight Rendering
- 透明背景-Transparent Background
- DOM
- 使用文檔-Working with Document
- 注入css-Injecting CSS
- 尋找元素-Finding Elements
- 元素屬性-Element Attributes
- 創建元素和文本節點-Creating Element & Text Node
- 設置節點值-Setting Node Value
- Select & Option Elements
- 選擇CheckBox-Selecting CheckBox
- Getting Selected Text
- 模擬點擊-Simulating Click
- DOM事件
- XPath
- 查詢選擇器-Query Selector
- 使用表單-Working with Form
- 滾動文檔-Scrolling Document
- 在Point處查找節點-Finding Node at Point
- 獲得元素界限-Getting Element Bounds
- 監聽內容變化-Listening to the Сontent Сhanges
- 模擬DOM事件-Simulating DOM Events
- Audio & Video
- MP3/MP4/H.264
- 網絡攝像頭和麥克風-Web Camera & Microphone
- 全屏視頻-Full Screen Video
- 靜音音頻-Muting Audio
- HTML5 Video
- Pop-ups
- 關于彈出窗口-About Pop-ups
- 在swing中處理彈出窗口-Handling Pop-ups Swing
- 在JavaFX中處理彈出窗口-Handling Pop-ups JavaFX
- Dialogs
- JavaScript對話框-JavaScript Dialogs
- 文件下載-File Download
- 上傳文件-File Upload
- 選擇SSL證書-Select SSL Certificate
- 選擇自定義SSL證書-Select Custom SSL Certificate
- 卸載前-Before Unload
- 顏色選擇器-Color Chooser
- Proxy
- 使用代理-Working with Proxy
- 系統代理設置-System Proxy Settings
- Authentication
- 處理代理驗證-Handling Proxy Authentication
- 處理基本,摘要和NTLM身份驗證-Handling Basic, Digest and NTLM Authentication
- JavaScript Java Bridge
- 從Java調用JavaScript-Calling JavaScript from Java
- 從JavaScript調用Java-Calling Java from JavaScript
- 控制臺消息-Console Messages
- 使用JSON-Working with JSON
- 使用jQuery-Working with jQuery
- 使用ScriptContext-Working with ScriptContext
- 將表單數據發送到Java-Sending Form Data to Java
- 使用數組-Working with Arrays
- @JSAccessible
- Plugins
- Printing
- Cookies
- Saving Web Page
- Zoom
- Integration
- Deploying
- Chromium
- Spell Checker
- Debugging
- Why JxBrowser
- Tips & Tricks
- 基礎知識
- AbstractAction
- Void
- SwingWorker應用詳解
- JAVA實現國際化
- UIManager
- AppJS
- heX
- bootstrap
- 知識點
- 空行
- Eclipse RCP
- Eclipse e4 概覽