Search code examples
javajxbrowser

JXBrowser cannot close JFrame


I am using JXBrowser 6.22.

Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        browser.loadURL("https://nf-test-jv.blogspot.com/");
        TimeUnit.SECONDS.sleep(20);
        browser.getDocument().findElement(By.cssSelector("#ReportAbuse1 > h3.title > a.report_abuse")).click();
        TimeUnit.SECONDS.sleep(10);
        frame.dispose();

When I execute the command click

browser.getDocument().findElement(By.cssSelector("#ReportAbuse1 > h3.title > a.report_abuse")).click();

The program will create a new frame pane enter image description here

When I execute frame.dispose(); it only closes the initial frame 1.Frame 2 doesn't close. How can I close everything when I want. Please help me


Solution

  • Check out the Frame.getFrames() method. It gives a list of all the created frames by the application.

    I would guess you could iterate through the Array to find the frame containing your Browser component and dispose() of that frame manually.

    You would need to add a WindowListener to your main frame and handle the windowClosing(...) method to implement the above logic.