Unable to Switch JPanels after loading jcef browser window

1k views Asked by At

I am trying to use jcef browser inside my swing application but getting problems. First of all i unable to add jcef browser as JPanel component on jFrame. Then i try to add directly on jframe [code]getContentPane().add(browser.getUIComponent(), BorderLayout.CENTER);[/code]

Now when browser window load inside JFrame, and If i want to switch with other Jpanel then it is not working working in any way I cant switch the screen after loading CEF browser. Can any one point out what i need to do. Here is my test jframe.

import org.cef.CefApp;
import org.cef.CefClient;
import org.cef.browser.CefBrowser;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class CardLayoutTst extends JFrame {
static CefBrowser browser = null;
static CefClient client = null;
private static final long serialVersionUID = 1L;
private JPanel cardPanel, jp1, jp2, buttonPanel;
private JLabel jl1, jl2;
private JButton btn1, btn2;
private CardLayout cardLayout = new CardLayout();

public CardLayoutTst() {
    setTitle("Test med CardLayout");
    setSize(400, 300);
    cardPanel = new JPanel();
    buttonPanel = new JPanel();
    cardPanel.setLayout(cardLayout);
    jp1 = new JPanel();
    jp2 = new JPanel();
    jl1 = new JLabel("Card 1");
    jl2 = new JLabel("Card 2");
    jp1.add(jl1);
    jp2.add(jl2);
    cardPanel.add(jp1, "1");
    cardPanel.add(browser.getUIComponent(), "2");
    btn1 = new JButton("Show Card 1");
    btn1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            cardLayout.show(cardPanel, "1");
        }
    });
    btn2 = new JButton("Show Card 2");
    btn2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            cardLayout.show(cardPanel, "2");
        }
    });
    buttonPanel.add(btn1);
    buttonPanel.add(btn2);
    add(cardPanel, BorderLayout.NORTH);
    add(buttonPanel, BorderLayout.SOUTH);

    addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            CefApp.getInstance().dispose();
            dispose();
        }
    });

}

public static void main(String[] args) {
    client = CefApp.getInstance().createClient();
    browser = client.createBrowser("http://www.google.com", false, false);

    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            CardLayoutTst frame = new CardLayoutTst();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    });
}

}

1

There are 1 answers

0
Constantin Butzke On

It is a bit late but I had just the same problem. A friend of mine solved the issue.

You have to create CefSettings, set the windowless_rendering_enabled to false and pass this settings object to the getInstance call:

CefSettings settings = new CefSettings();
settings.windowless_rendering_enabled = false;

client = CefApp.getInstance(settings).createClient();