kordamp / ikonli

Icon packs for Java applications
http://kordamp.org/ikonli/
Apache License 2.0
502 stars 50 forks source link

Swing and JavaFX FontIcons no longer work in same application as of 12.0.0 #127

Closed eric-upchurch closed 3 years ago

eric-upchurch commented 3 years ago

With versions 11.5.0 and previous, you could utilize both Swing and JavaFX Ikonli FontIcons in the same application without problems, and easily convert between them by retrieval of the Ikon.getIconCode() and passing it into the respective FontIcon.of() methods of either the Swing or JavaFX FontIcon classes.

With 12.0.0, once you use either a JavaFX or Swing FontIcon, the IkonHandlers for each will return the font class of the first used FontIcon type. Using the other (e.g. using JavaFX and then Swing) will cause a CastClassException because the IkonResolver for each type will return the same Font class - if you use SwingFontIcon first, it will return java.awt.Font; if you use JavaFX FontIcon first, it will return javafx.scene.text.Font. This prevents you from using both JavaFX and Swing FontIcons within the same application.

This can be demonstrated with the following code segment:

public class IkonliFxWithSwing {
  public static void main(String[] args) {
    org.kordamp.ikonli.swing.FontIcon swingIcon = org.kordamp.ikonli.swing.FontIcon.of(Ikonli.NONE);
    org.kordamp.ikonli.javafx.FontIcon fxIcon = org.kordamp.ikonli.javafx.FontIcon.of(Ikonli.NONE);

    IkonHandler fxHandler = org.kordamp.ikonli.javafx.IkonResolver.getInstance().resolve(fxIcon.getIconCode().getDescription()); // change to resolveIkonHandler() for Ikonli 11.5.0
    System.out.println("JavaFX Font: " + fxHandler.getFont().getClass().getName() + " -> " + fxHandler.getFont());

    IkonHandler swingHandler = org.kordamp.ikonli.swing.IkonResolver.getInstance().resolve(swingIcon.getIkon().getDescription());
    System.out.println("Swing Font: " + swingHandler.getFont().getClass().getName() + " -> " + swingHandler.getFont());
  }
}
aalmiray commented 3 years ago

Hmm I believe Ikonli was not designed to have both Swing and JavaFX icons resolved in the same application. It might have worked like that by mistake. This being said, I'll see if it's possible to officially support this setup.

eric-upchurch commented 3 years ago

Thanks for looking into it! If possible, it would great to have that officially supported for our use case since we still have some components that require Swing in an otherwise fully JavaFX UI.

aalmiray commented 3 years ago

FWIW the problem stems from https://github.com/kordamp/ikonli/commit/c78b2d22840fce2c0bc4c690134b690711d99ced where static members that were previously private to each IkonResolver type were pushed upward to a common ancestor. As I said, it was pure coincidence the behavior you seek actually worked, I know now how to fix it so that it works for good 😄