Use SceneBuilder with JavaFX 11 in IDEA

后端 未结 3 1389
花落未央
花落未央 2021-01-15 02:35

I am using IDEA (with OpenJDK 11), and I am trying to use the SceneBuilder to display my FXML files. It works, except when I use inherited JavaFX components, for example :

相关标签:
3条回答
  • 2021-01-15 03:29

    Just try to install plugin "choose runtime" in the newest EAP 2019.3 version for Java 11 and CTRL-SHIFT-A choose choose-runtime and latest jbrsdk-8. Choose in your project structure java 11, but with java 8 compatibility.

    Also use jfoenix 8.0.9 in your project to run scene builder. To run program use jfoenix 9.0.9.

    0 讨论(0)
  • 2021-01-15 03:33

    In addition to José Pereda's answer,

    in a Maven project, you need to set build > plugins > maven-compiler-plugin > configuration > release to 8, instead of 11

    0 讨论(0)
  • 2021-01-15 03:35

    If IntelliJ's embedded Scene Builder version is 8.2, then you can't expect that it can "render" any class in its classpath compiled for a Java version greater than 8 (level 52).

    The embedded Scene Builder is just another Java application running, basically a JavaFX application running embedded in a Swing component on top of a IntelliJ editor.

    Java 8

    In order to work with regular FXML files that use built-in controls, Scene Builder uses the JDK to load the jfxrt.jar to the classpath.

    If you create your own custom control using Java 8 (this question has a good sample on how to do this), and you don't compile the project, when you try Scene Builder you will get a ClassNotFoundException, because the control is not in the classpath, and Scene Builder doesn't know how to deal with it.

    But after you build the project, the resulting classes are added to the classpath as well, and you can see the control rendered.

    Java 11

    When you create the custom control with Java 11, as you know, there is no more jfxrt.jar.

    Following the Getting Started docs, you need to add the JavaFX 11 SDK to your project, and the VM options

    --module-path ${PATH_TO_FX} --add-modules=javafx.controls,javafx.fxml
    

    in order to run the project.

    If you have a regular FXML file, with just built-in controls, and go to the embedded Scene Builder, it will load and render it. Why is that? Because it is probably using the internal JDK 1.8 shipped with IntelliJ, so the FXML file (containing pure xml, not a java file!) can be parsed by the (Java 8) FXMLLoader and rendered.

    But if you try your custom control, after building the project, you will get the exception you mentioned:

    since you are compiling with Java 11, and the level now is 55.

    Is there any way to make it work? As mentioned before, all you need to do is load your control into the classpath, but with 52 level.

    So all you need to do is set the target level of your project to 1.8, in Project Structure -> Project -> Project Language level

    Now build again your project, and you will be able to see the custom control with the embedded Scene Builder:

    Since your project is now compatible with Java 8, you can verify that you will be able to run:

    /path-to-java-8/java -jar out/production/Custom_jar/Custom.jar
    

    In any case, if you don't want that level, you can of course keep Java 11 and use the stand-alone Scene Builder 11.

    0 讨论(0)
提交回复
热议问题