简体   繁体   中英

How to resize the children WebView/JFXPanel from the JPanel without loading URL every time?

I have a JPanel in a TabbedPane , that contains a JFXPanel with a WebView and I set a ComponentListener on the panel, in order to resize its child, the JFXPanel with the WebView , every time I resize the panel. What I want is that the content from the URL (a website) does not get loaded every time I resize the panel. I want the content to remain loaded and only get resized.

The content that is being loaded, is the homepage of a website and for example when I resize the panel, I am being redirected to that homepage, that I set as URL and I don't want to lose the page, that I currently open.

The second reason for avoiding the reloading is that sometimes it lasts for a few seconds and I would like to avoid this overload.

I tried with a static variable initially in order to open the URL just one time, but when resizing, the page isn't being shown anymore...there appears only a white page.

Here is the code:

public class RtcOverview extends JPanel {

String url = "http://10.112.85.142:8080/petshopJSF/";

public RtcOverview() {
    super();
    this.setVisible(true);
    this.doLayout();
    this.add(jfxPanel);
    this.addComponentListener(new java.awt.event.ComponentAdapter() {
        public void componentResized(ComponentEvent e) {
            initComponents();
        }
    });
}

private void initComponents() {
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            final WebView view = new WebView();
            int width = getParent().getWidth();
            int height = getParent().getHeight();

            view.setMinSize(width, height);
            view.setPrefSize(width, height);

            engine = view.getEngine();
            engine.load(url);

            Scene scene = new Scene(view);
            jfxPanel.setScene(scene);

            Platform.setImplicitExit(false);
        }
    });
}
}

Andrew Thompson gave me this answer in the comment above. There is no need to call the method initComponents() every time the JPanel gets resized. Instead of using this block

this.addComponentListener(new java.awt.event.ComponentAdapter() {
    public void componentResized(ComponentEvent e) {
        initComponents();
    }
});

I used the following

this.setLayout(new BorderLayout());

this.add(jfxPanel, BorderLayout.CENTER);

and it worked. The JFXPanel gets resized like its parent JPanel without needing to load the url again and initialize all the variables every single time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM