'Exception java.lang.ClassNotFoundException: javafx.embed.swing.SwingNode when executing a GUI for a database query, results in a SwingNode

I am a beginner in using FX and JDBC. I am trying to implement a GUI which enable the user to write a query and view the answers in a table. I am using BorderPane to hold the GUI components and a SwingNode to display the results in a JTable which is populated from a subclass of AbstractTableModel.The SwingNode is supposed to be created when the app is initialized and is going to be recreated everytime the user enter a query string. I am using FXML to define the GUI.It seems that even when I comment the block statements which assign the SwinNode content and just let the statement of declaration of the SwingNode object, the exception is thrown from the Application class. Please allow me to show the FXML file.

<?xml version="1.0" encoding="UTF-8"?>

<!-- <?import javafx.embed.swing.SwingNode?> -->
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<BorderPane fx:id="borderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ExecQuery.DisplayQueryResultsController">
   <top>
      <GridPane hgap="10.0" maxWidth="1.7976931348623157E308" BorderPane.alignment="CENTER">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="485.0" minWidth="10.0" prefWidth="475.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="293.0" minWidth="10.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#submitQueryButtonPressed" prefHeight="25.0" prefWidth="151.0" text="Submit Query" GridPane.columnIndex="1" />
            <TextArea fx:id="queryTextArea" maxWidth="1.7976931348623157E308" prefRowCount="3" promptText="Enter query here then press Submit Query" wrapText="true" />
         </children>
         <padding>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </padding>
      </GridPane>
   </top>
   <bottom>
      <GridPane hgap="10.0" BorderPane.alignment="CENTER">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="130.0" minWidth="70.0" prefWidth="70.0" />
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="363.0" minWidth="10.0" prefWidth="363.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="201.0" minWidth="10.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#applyFilterButtonPressed" text="Apply Filter" GridPane.columnIndex="2" />
            <Label text="Enter filter text:" />
            <TextField fx:id="filterTextField" prefHeight="25.0" prefWidth="332.0" GridPane.columnIndex="1" />
         </children>
      </GridPane>
   </bottom>
   <padding>
      <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
   </padding>
   <!--<left>
      <SwingNode fx:id="swingNode" BorderPane.alignment="CENTER" />
   </left> -->
</BorderPane>

I have the main application which initialize the FXML GUI

package ExecQuery;

import javafx.embed.swing.SwingNode;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene; 
import javafx.stage.Stage;

public class DisplayQuery extends Application{
    
    public static void main(String [] args)
    {
        launch(args);
        
    }
    
    @Override
    public void start(Stage teatri) throws Exception
    {
        Parent rrenje= FXMLLoader.load(getClass().getResource("DisplayQueryResults.fxml"));
        Scene skene=new Scene(rrenje);
        teatri.setTitle("Execute Query");
            
        teatri.setScene(skene);
        teatri.show();
        
    }
    
    }

Here I have part of the controller class which create a SwingNode object and insert into the BorderPane object.

package ExecQuery;

import javafx.embed.swing.SwingNode;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;


import java.sql.SQLException;
import java.util.regex.PatternSyntaxException;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;


public class DisplayQueryResultsController {

    @FXML     private BorderPane borderPane;
//    @FXML   private SwingNode swingNode;
    @FXML     private TextField filterTextField;
    @FXML     private TextArea queryTextArea;
    
    private final String DATABASE_URL="jdbc:mysql://localhost/publicbudget";
    private final String USERNAME="root";
    private final String PASSWORD="";
    
    private final String DEFAULT_QUERY="SELECT * FROM expenditurebudgeted";
    //variabla per te konfiguruar JTable
    private ResultSetTableModel tableModel;
    //private TableRowSorter<TableModel> sorter;

    public void initialize()
    {
        queryTextArea.setText(DEFAULT_QUERY);
        
    try {
        
        tableModel=new ResultSetTableModel(DATABASE_URL, USERNAME, PASSWORD,DEFAULT_QUERY);
        
        JTable tabelRezultat=new JTable(tableModel);
                
        
        SwingNode swingNode=new SwingNode();
        swingNode.setContent(new JScrollPane(tabelRezultat));
        borderPane.setCenter(swingNode);
    
        
    }
    catch(SQLException sqlE)
    {
        displayAlert(AlertType.ERROR, "Database Error", sqlE.getMessage());
        tableModel.disconnectFromDatabase();
        System.exit(1);
        
    }
        
        
    }
    
    @FXML
    void applyFilterButtonPressed(ActionEvent event) {
        
    }

    @FXML
    void submitQueryButtonPressed(ActionEvent event) {
        
            }
    
    public void displayAlert(AlertType type, String errorTitle, String errorMessage)
    {
        
    }

}

When I execute the Application subclass, it shows this error message

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.NoClassDefFoundError: javafx/embed/swing/SwingNode
    at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3215)
    at java.base/java.lang.Class.getConstructor0(Class.java:3420)
    at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2631)
    at [email protected]/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:939)
    at [email protected]/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:981)
    at [email protected]/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:230)
    at [email protected]/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:755)
    at [email protected]/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2808)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2634)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3331)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
    at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
    at [email protected]/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
    at ExecQuery.DisplayQuery.start(DisplayQuery.java:23)
    at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at [email protected]/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at [email protected]/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at [email protected]/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    ... 1 more
Caused by: java.lang.ClassNotFoundException: javafx.embed.swing.SwingNode
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 27 more
Exception running application ExecQuery.DisplayQuery

Anyone can help me with this error? I would appreciate any suggestion. I am using javafx-sdk-18.0.1.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source