0

I am currently writing a JavaFX program in Code Runner on my Mac. When I type in new elements, such as buttons or panes, code runner automatically puts in an import for awt like this

import java.awt.*;

It does this even if I have already imported the correct statement from javafx, for example

import javafx.scene.control.Button;

When I type in Button bt = new Button("hello"); Code Runner automatically puts in the import java.awt.*;

Is there a way to turn this off? I have looked in all of the settings for Code Runner and my preferences for java, but I can't see anything.

Dallen Corry
  • 23
  • 1
  • 7
  • I don't know anything about CodeRunner or how it works. But you could create a [custom java runtime using jlink](https://www.baeldung.com/jlink) which does not include awt/swing modules, but does include JavaFX modules, and use that. Then, think it is unlikely that CodeRunner would find awt/swing classes to populate imports instead of JavaFX classes. – jewelsea Nov 10 '21 at 19:48
  • @jewelsea I'm not sure that's possible, as I believe even `javafx.base` has a dependency on `java.desktop` (unfortunately). – Slaw Nov 10 '21 at 22:15
  • @Slaw yes you are correct. I didn't realize that. That's a shame. It means that the base JavaFX code has a required dependency on all of the legacy AWT/Swing code. So you can't build a JavaFX app without including AWT/Swing. The [javafx.base](https://openjfx.io/javadoc/17/javafx.base/module-summary.html) module [requires](https://github.com/openjdk/jfx/blob/6749ab60b7673a0838d55fbd09cabf4232d5da60/modules/javafx.base/src/main/java/module-info.java#L34) the [java.desktop](https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/module-summary.html). – jewelsea Nov 10 '21 at 22:46
  • It also means my proposed solution won't work, as it is impossible to use jlink to build a custom runtime for java + JavaFX which does not also include awt + swing (at least as far as I can tell). – jewelsea Nov 10 '21 at 22:49
  • Not that it helps you, but the feature you request is available in Intellij Idea: [Exclude classes and packages from auto import](https://www.jetbrains.com/help/idea/creating-and-optimizing-imports.html#exclude-from-auto-import) – jewelsea Nov 10 '21 at 22:52
  • @jewelsea Yup, and it really is unfortunate. As far as I can tell, the only bit in `javafx.base` that is dependent on code in `java.desktop` is the `javafx.beans.property.adapter` classes. Ideally, the only module that should have been dependent on `java.desktop` is the `javafx.swing` module. Honestly, they should have put the `javafx.beans.property.adapter` classes either in `javafx.swing` or in their own module. And I realize they probably wanted to avoid having too many modules, but there should have been separate `java.beans`, `java.awt`, and `java.swing` modules, at least in my opinion. – Slaw Nov 10 '21 at 23:00
  • @jewelsea that actually is very useful, as I was considering switching my main IDE anyway. But thank you both for the insights! – Dallen Corry Nov 10 '21 at 23:09
  • @Slaw there are some cases and [dev discussion](https://www.mail-archive.com/openjfx-dev@openjdk.java.net/msg22337.html) to update the module dependencies for javafx-base and further [split it up](https://bugs.openjdk.java.net/browse/JDK-8239853), as you suggest, so the [base JavaFX system doesn't have a hard dependency on AWT/Swing](https://bugs.openjdk.java.net/browse/JDK-8240844). Also for javafx-graphics which is the core JavaFX framework code and depends on AWT/Swing internally for printing support. Hopefully, the hard dependencies will be removed in future releases. – jewelsea Nov 11 '21 at 00:15

0 Answers0