I create a program with JCEF that displays multiple browser but it's not working fine.
For example, launch the program and displays two separate Google pages,
CPU usage gets 100% (program 50% and CTF loader 50%) and if I focus Google's search field caret rapidly flickering.
It doesn't happen with single browser.
here is the code below.
- Code: Select all
public class MainFrame extends JFrame {
private static final long serialVersionUID = -5570653778104813836L;
private final JTextField address_;
private final CefApp cefApp_;
private final CefClient client_;
private final CefBrowser browser_;
private final Component browerUI_;
private final CefBrowser browser_2;
private final Component browerUI_2;
private boolean browserFocus_ = true;
public static void main(String[] args) {
// Perform startup initialization on platforms that require it.
if (!CefApp.startup(args)) {
System.out.println("Startup initialization failed!");
return;
}
// The simple example application is created as anonymous class and points
// to Google as the very first loaded page. Windowed rendering mode is used by
// default. If you want to test OSR mode set |useOsr| to true and recompile.
boolean useOsr = false;
new MainFrame("http://www.google.com", useOsr, false);
}
/**
* To display a simple browser window, it suffices completely to create an
* instance of the class CefBrowser and to assign its UI component to your
* application (e.g. to your content pane).
* But to be more verbose, this CTOR keeps an instance of each object on the
* way to the browser UI.
*/
private MainFrame(String startURL, boolean useOSR, boolean isTransparent) {
CefApp.addAppHandler(new CefAppHandlerAdapter(null) {
@Override
public void stateHasChanged(org.cef.CefApp.CefAppState state) {
// Shutdown the app if the native CEF part is terminated
if (state == CefAppState.TERMINATED)
System.exit(0);
}
});
CefSettings settings = new CefSettings();
settings.windowless_rendering_enabled = useOSR;
cefApp_ = CefApp.getInstance(settings);
client_ = cefApp_.createClient();
browser_ = client_.createBrowser(startURL, useOSR, isTransparent);
browerUI_ = browser_.getUIComponent();
//second browser
browser_2 = client_.createBrowser(startURL, useOSR, isTransparent);
browerUI_2 = browser_2.getUIComponent();
address_ = new JTextField(startURL, 100);
address_.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
browser_.loadURL(address_.getText());
}
});
// Update the address field when the browser URL changes.
client_.addDisplayHandler(new CefDisplayHandlerAdapter() {
@Override
public void onAddressChange(CefBrowser browser, CefFrame frame, String url) {
address_.setText(url);
}
});
// Clear focus from the browser when the address field gains focus.
address_.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
if (!browserFocus_)
return;
browserFocus_ = false;
KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
address_.requestFocus();
}
});
// Clear focus from the address field when the browser gains focus.
client_.addFocusHandler(new CefFocusHandlerAdapter() {
@Override
public void onGotFocus(CefBrowser browser) {
if (browserFocus_)
return;
browserFocus_ = true;
KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
browser.setFocus(true);
}
@Override
public void onTakeFocus(CefBrowser browser, boolean next) {
browserFocus_ = false;
}
});
getContentPane().add(address_, BorderLayout.NORTH);
JPanel jpanel1 = new JPanel();
JPanel jpanel2 = new JPanel();
jpanel1.setLayout(new BorderLayout());
jpanel2.setLayout(new BorderLayout());
jpanel1.setBounds(0, address_.getHeight(), 400, 600-address_.getHeight());
jpanel2.setBounds(400, address_.getHeight(), 400, 600-address_.getHeight());
jpanel1.add(browerUI_);
jpanel2.add(browerUI_2);
pack();
setSize(800, 600);
getContentPane().add(jpanel1);
getContentPane().add(jpanel2);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
CefApp.getInstance().dispose();
dispose();
}
});
}
}
if you have any ideas, please share.
thanks.