JCEF BrowserUI not displaying in JFrame or Ja

Having problems with building or using the JCEF Java binding? Ask your questions here.

JCEF BrowserUI not displaying in JFrame or Ja

Postby TeslaK20 » Fri Apr 19, 2019 5:31 am

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?
TeslaK20
Newbie
 
Posts: 2
Joined: Fri Apr 19, 2019 5:29 am

Re: JCEF BrowserUI not displaying in JFrame or Ja

Postby magreenblatt » Fri Apr 19, 2019 10:42 am

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.
magreenblatt
Site Admin
 
Posts: 11093
Joined: Fri May 29, 2009 6:57 pm

Re: JCEF BrowserUI not displaying in JFrame or Ja

Postby TeslaK20 » Wed Apr 24, 2019 9:11 am

Why would JCEF not run when called from inside a JFrame class?
TeslaK20
Newbie
 
Posts: 2
Joined: Fri Apr 19, 2019 5:29 am

Re: JCEF BrowserUI not displaying in JFrame or Ja

Postby johnrdorazio » Wed Oct 14, 2020 3:08 am

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...
johnrdorazio
Newbie
 
Posts: 5
Joined: Wed Oct 14, 2020 3:04 am

Re: JCEF BrowserUI not displaying in JFrame or JInternalFram

Postby johnrdorazio » Thu Oct 22, 2020 9:41 am

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.
johnrdorazio
Newbie
 
Posts: 5
Joined: Wed Oct 14, 2020 3:04 am


Return to JCEF Forum

Who is online

Users browsing this forum: No registered users and 1 guest