Search code examples
javaswingjxbrowser

Java swing controls and JxBrowser page in the same frame?


I can't seem to find any information on this, is it possible to use JxBrowser to display a webpage and java swing controls within the same frame and if so, how? My current code is just one to display a window with google maps currently as I keep trying things and deleting them.

public static void main(String[] args) {
    Browser browser = new Browser();
    BrowserView view = new BrowserView(browser);
    JFrame frame = new JFrame("Google Maps");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(1500,1000);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        browser.loadURL("http://maps.google.com");
}

Solution

  • BrowserView that you embed into a frame is a Swing component. You can add other Swing components in the same way:

    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);
    
        JFrame frame = new JFrame("Google Maps");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
    
        // Let's add a button
        frame.add(new JButton("My Button"), BorderLayout.SOUTH);
    
        frame.setSize(1500,1000);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    
        browser.loadURL("http://maps.google.com");
    }