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

I have JPanel

in TabbedPane

that contains JFXPanel

c WebView

and I have set a ComponentListener

on the panel to resize its child JFXPanel

with WebView

, every time I resize the panel. I want to prevent the url (website) content from loading every time the panel is resized. I want the content to stay loaded and only change.

The downloadable content is the home page of the website and, for example, when the panel is resized, I am redirected to this home page, which I have set as a URL, and I do not want to lose the page that I am currently open.

The second reason to avoid a reboot is that sometimes it takes a few seconds, and I would like to avoid this overload.

I first tried using a static variable to open the url just once, but when resized, the page doesn't show anymore ... only a white page appears.

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);
        }
    });
}
}

      

+3


source to share


1 answer


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

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

      

I have used the following



this.setLayout(new BorderLayout());

this.add(jfxPanel, BorderLayout.CENTER);

      

and it worked. The JFXPanel gets the size just like its parent JPanel, without having to load the url again and initialize all the variables every time.

+1


source







All Articles