Search code examples
javahtmljframejpaneljxbrowser

Adding JxBrowser Content to JPanel


Is it not possible to add jxbrowser contents to jpanel. When I add jxBrowser content to JFrame, it is seen very easily. But I am not able to add jxBrowser content to jpanel. I don't know if it is not possible or I am not able to add jxBrowser contents to jpanel.


Solution

  • Here's sample code that demonstrates how to embed JxBrowser Browser component into JPanel:

    import com.teamdev.jxbrowser.chromium.Browser;
    import com.teamdev.jxbrowser.chromium.swing.BrowserView;
    
    import javax.swing.*;
    import java.awt.*;
    
    /**
     * This sample demonstrates how to create Browser instance,
     * embed it into Swing BrowserView container, display it in JFrame and
     * navigate to the "www.google.com" web site.
     */
    public class BrowserSample {
        public static void main(String[] args) {
            Browser browser = new Browser();
            BrowserView view = new BrowserView(browser);
    
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(view, BorderLayout.CENTER);
    
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.add(panel, BorderLayout.CENTER);
            frame.setSize(700, 500);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
    
            browser.loadURL("https://google.com");
        }
    }
    

    More samples you can find at https://jxbrowser.support.teamdev.com/support/solutions/9000049010