0

I'm developing an application using Java Swing on JDK8 which brings the JavaFX inside. I need to show the JavaFX Webview inside a JPanel. I've used this example provided by Oracle and it worked fine! But, when I try to use this inside my application, the webview doesn't appear. I'm using Netbeans GUI Form Builder and I think this is the problem.

Is there a way to display this Webview without going to write the whole screen manually?

Edit: Sorry, I've forgot to copy the example... This is the form:

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

public class WebviewInSwing extends javax.swing.JFrame {

    private String url = "https://google.com";
    private JFXPanel jfxpanel = new JFXPanel();
    private WebEngine engine;
    
    public WebviewInSwing() {
        initComponents();
        
        Platform.runLater(() -> {
            WebView webview = new WebView();
            engine = webview.getEngine();
            engine.load(url);
            jfxpanel.setScene(new Scene(webview));
        });
        
        jPanel1.add(jfxpanel);
        
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 605, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 360, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new WebviewInSwing().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                   
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
lelos
  • 13
  • 2
  • I'm not familiar with `GroupLayout`, but it seems you have to add components via the layout, not using `jPanel1.add(jfxpanel)`. So, if you use `GroupLayout`, you'd need to create the `JFXPanel` in the UI designer tool you're using. It might be easier to use a more straightforward layout (e.g. `BorderLayout`) for `jPanel1`. – James_D Jan 06 '22 at 15:41
  • Hi @James_D! It Worked! Thanks!! – lelos Jan 06 '22 at 16:15
  • `GroupLayout` seems unwarranted; it may be an artifact of using a GUI editor; this [example](https://stackoverflow.com/a/31576647/230513) explicitly specifies the frame's implicit `BorderLayout.CENTER`. – trashgod Jan 06 '22 at 17:48
  • Hi @trashgod! The netbeans default layout is `GroupLayout`, so I don't mind to change. This is the first time I need to change the layout to use something. Changing the JPanel layout to `BorderLayout` worked on the first try! Thanks for the example! – lelos Jan 06 '22 at 18:52
  • `GroupLayout` is the GUI editor's default; the default layout of `JFrame` is `BorderLayout` and the default position is `BorderLayout.CENTER`; more [here](https://stackoverflow.com/a/2561540/230513). – trashgod Jan 06 '22 at 19:33

0 Answers0