Jcef How to modify the right-click menu item (JCEF Add Developer options in the right-click menu-show dev Tools)

Source: Internet
Author: User

Demand:

Add a menu to open developer options in the right-click event of the interface (Show Dev Tools)

So what we can see is the right button on the interface, there will be some default menu items (forward, rewind, print, view source), and these features are not necessarily what we need, so the first thing we have to do is to remove these options, reference: Java CEF3 How to Prohibit right-click menu items

Similarly, we only need to customize our menu item in the implementation class of the Cefcontextmenuhandler interface, the code is as follows:

 Public classContextmenuhandlerImplementsCefcontextmenuhandler {Private FinalFrame owner;  PublicContextmenuhandler (Frame owner) { This. Owner =owner; } @Override Public voidOnbeforecontextmenu (Cefbrowser browser, cefcontextmenuparams params, Cefmenumodel model) {model.clear ();//Clear the Default menu itemModel.additem (Menuid.menu_id_show_dev_tools, "Developer Options"); } @Override Public BooleanOncontextmenucommand (Cefbrowser Browser, cefcontextmenuparams params,intCommandId,inteventflags) {        Switch(commandId) { CaseMenuid.menu_id_show_dev_tools: {//Open Developer OptionsDevtoolsdialog Devtoolsdlg =NewDevtoolsdialog (owner, "developer Options", browser); Devtoolsdlg.setvisible (true); return true; }            default: {                return false; } }} @Override Public voidoncontextmenudismissed (Cefbrowser browser) {}}

In Onbeforecontextmenu first clear the default menu items, and then bind the developer options, it is important to note that menuid.menu_id_show_dev_tools this does not exist in the MenuId, we need to manually add in

Then we bind the menu event in the Oncontextmenucommand method, then restart the server, right-click on the interface to see the developer options, open the Developer Options dialog box

The implementation of the attached Devtoolsdialog:

 Public classDevtoolsdialogextendsJDialog {Private Static Final LongSerialversionuid = 6859581641415822180L; Private FinalCefbrowser Devtools_;  PublicDevtoolsdialog (Frame owner, String title, Cefbrowser browser) { This(owner, title, browser,NULL); }     PublicDevtoolsdialog (Frame owner, String title, Cefbrowser Browser, point Inspectat) {Super(Owner, title,false); SetLayout (NewBorderLayout ()); SetSize (800, 600); SetLocation (Owner.getlocation (). x+, Owner.getlocation (). Y + 20); Devtools_=Browser.getdevtools (Inspectat);        Add (Devtools_.getuicomponent ()); Addcomponentlistener (NewComponentadapter () {@Override Public voidComponenthidden (ComponentEvent e) {Dispose ();    }        }); } @Override Public voidDispose () {devtools_.close (); Super. Dispose (); }}
View Code

Jcef How to modify the right-click menu item (JCEF Add Developer options in the right-click menu-show dev Tools)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.