简体   繁体   中英

How to fix exception when creating JFXPanel in Swing with JavaFX 12

I have a large Java application with a Swing-based UI that uses JavaFX to render video panels and WebView panels in parts of some windows via JFXPanel components.

Everything worked fine with JDK 8 but I am migrating to OpenJDK 12 and JavaFX 12 and am getting a runtime exception when creating a JFXPanel. When I call "new JFXPanel()" I am getting the exception:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalAccessError: class 
javafx.embed.swing.JFXPanel (in unnamed module @0x44bebd15) cannot access 
class com.sun.javafx.logging.PlatformLogger (in module javafx.base) because 
module javafx.base does not export com.sun.javafx.logging to unnamed module 
@0x44bebd15

The javafx.base and javafx.swing jar files from javafx-sdk-12.0.2 are included in my Eclipse project and runtime (along with a few other JavaFX jars).

I am using VM arguments: --module-path /Library/Java/javafx-sdk-12.0.2/lib --add-modules javafx.controls,javafx.web,javafx.media,javafx.base

Is this a bug in JavaFX 12 or am I doing something wrong?

htmlPanel = new JFXPanel() {
    public Dimension getPreferredSize() {                                              
    return new Dimension(CoreDrawer.scaledSize(width), 
    CoreDrawer.scaledSize(height));                             
        }

    public Dimension getMinimumSize() 
      {
    return getPreferredSize();                  
    }
  };

  Platform.runLater(new Runnable() {    //  run on the JavaFX thread                                                             
  public void run() {                                                                
  initFX(htmlPanel);                                                             
    }
  });

  Platform.setImplicitExit(false);
  thePanel.add(htmlPanel);

The function initFX() adds a Scene to the JFXPanel but the program does not get past "new JFXPanel()"

Comment 'Use --add-modules javafx.web,javafx.media,javafx.swing' by Slaw worked for me. Needed to append javafx.swing to 'VM options' of my run configuration.

public class Main extends Application{

    @Override
    public void start(Stage primaryStage) throws Exception {



        primaryStage.initStyle(StageStyle.TRANSPARENT);
        Parent root=FXMLLoader.load(Main.class.getResource("../../../com/chatbot/view/chatgui.fxml"));
        Scene scene=new Scene(root);
        scene.setFill(Color.TRANSPARENT);
        primaryStage.setScene(scene);
        primaryStage.show();

    }


    public static synchronized void main(String[] args) throws InterruptedException {

                InterfaceGraphiqueAuth fen = new InterfaceGraphiqueAuth();



            while(!fen.getFx()){

                try{
                    Thread.currentThread().wait();;
                }catch(Exception exc){}

                }


        launch(args);

    }
}

I couldn't fix the exception so i used this alternative. InterfaceGraphiqueAuth is a swing application when it ends, it sets the boolean which i named fx to false so getFx returns false and the javafx window opens. I added the wait method to wait for the swing application to set the boolean fx to false.

This is what happens in InterfaceGraphiqueAuth :

if((loginTF.getText().compareTo(compte.getUsername()) == 0)&& (passwordTF.getText().compareTo(compte.getCode()) == 0)){ 
                            //le compte existe dans la base de donn�es
                            k=true;
                            prenom=compte.getPrenom();
                            lastname=compte.getNom();
                            sexe=compte.getSexe();
                        }
                    }
                            if(k){
                                dispose();

                                    fx=true;

                                   }

                        else JOptionPane.showMessageDialog(null, "Echec");

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