Page 1 of 1

JCEF BrowserUI not displaying in JFrame or Ja

PostPosted: Fri Apr 19, 2019 5:31 am
by TeslaK20
I've been trying to add the JCEF browser to a JFrame created with the Swing UI Builder in NetBeans. Unfortunately, if I try to add it to a JFrame or JPanel I create using this tool, it does not work. The Swing window appears, but it is simply empty.

Code: Select all
final CefApp cefApp = CefApp.getInstance();
final CefClient client = cefApp.createClient();
final CefBrowser browser = client.createBrowser("http://www.google.com", OS.isWindows(), false);
final Component browserUI = browser.getUIComponent();
getContentPane().add(browserUI, BorderLayout.CENTER);

The same happens with a JPanel.

Code: Select all
jPanel1.add(browserUI, BorderLayout.CENTER);

However, when I programatically initiate a new, separate JFrame window, it works perfectly.

Code: Select all
final JFrame mainFrame = new JFrame();
getContentPane().add(browserUI, BorderLayout.CENTER);
mainFrame.setSize(800, 600);
mainFrame.setVisible(true);

Why is this happening? How can I get it to work in any other JFrame or JPanel?

Re: JCEF BrowserUI not displaying in JFrame or Ja

PostPosted: Fri Apr 19, 2019 10:42 am
by magreenblatt
I suggest you look at the code for the "JFrame created with the Swing UI Builder in NetBeans" and compare it to the JFrame that you're creating programatically.

Re: JCEF BrowserUI not displaying in JFrame or Ja

PostPosted: Wed Apr 24, 2019 9:11 am
by TeslaK20
Why would JCEF not run when called from inside a JFrame class?

Re: JCEF BrowserUI not displaying in JFrame or Ja

PostPosted: Wed Oct 14, 2020 3:08 am
by johnrdorazio
I was having the same problem. I solved it by calling setLayout(new BorderLayout()) before getContentPane():
Code: Select all
        setLayout(new BorderLayout());
        getContentPane().add(browserUI, BorderLayout.CENTER);

I don't know all the ins and outs of Netbeans designer mode, but for some reason this is not needed in a standalone application (as can be seen in the "simple" and "detailed" examples included with JCEF) whereas it is needed in Netbeans when building a Frame in designer mode...

Re: JCEF BrowserUI not displaying in JFrame or JInternalFram

PostPosted: Thu Oct 22, 2020 9:41 am
by johnrdorazio
johnrdorazio wrote:I was having the same problem. I solved it by calling setLayout(new BorderLayout()) before getContentPane():
Code: Select all
        setLayout(new BorderLayout());
        getContentPane().add(browserUI, BorderLayout.CENTER);



Actually I've now realized that if you right-click on the jFrame (or jInternalFrame ) component in Design View, you can set the layout to Border Layout from the Set Layout context menu option. In which case you don't have to manually call setLayout(new BorderLayout()). Since Border Layout is considered the default option, this will probably remove any call to setLayout() from the InitComponents() section.