JFormDesigner / FlatLaf

FlatLaf - Swing Look and Feel (with Darcula/IntelliJ themes support)
https://www.formdev.com/flatlaf/
Apache License 2.0
3.42k stars 272 forks source link

Exception when trying to print JComponent #462

Closed bkuliha closed 2 years ago

bkuliha commented 2 years ago

When using FlatLaf LaF, the attached code fails on ClassCastException. When using default LaF the code works fine.

Tested with lastest version 2.0-rc1.

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.print.ProxyPrintGraphics cannot be cast to java.awt.Graphics2D
    at com.formdev.flatlaf.ui.FlatBorder.paintBorder(FlatBorder.java:85)
    at javax.swing.JComponent.paintBorder(JComponent.java:949)
    at javax.swing.JComponent.printBorder(JComponent.java:1246)
    at javax.swing.JComponent.paint(JComponent.java:1061)
    at javax.swing.JComponent.print(JComponent.java:1202)
    at javax.swing.JComponent.paintChildren(JComponent.java:893)
    at javax.swing.JComponent.printChildren(JComponent.java:1233)
    at javax.swing.JComponent.paint(JComponent.java:1068)
    at javax.swing.JComponent.print(JComponent.java:1202)
    at PrintingDemo$PrintJobExample.actionPerformed(PrintingDemo.java:61)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6539)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6304)
    at java.awt.Container.processEvent(Container.java:2239)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2297)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
    at java.awt.Container.dispatchEventImpl(Container.java:2283)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
    at java.awt.EventQueue$4.run(EventQueue.java:733)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

PrintingDemo.java.txt

DevCharly commented 2 years ago

Thanks for reporting and for the demo.

Unfortunately this is not easy to fix. FlatLaf painting depends a lot on class Graphics2D and there are many places where Graphics is simply cast to Graphics2D.

I wonder what's the use case for printing Swing UI ?

bkuliha commented 2 years ago

I do not really have a use case for you. I work at Webswing and we use FlatLaF in our demo application to showcase different components and use cases that we support. Component printing is part of one of our demos, so that is how we identified the issue. Right now we do not have any direct complaints from our customers about this issue. So we do not really see this as a priority. But at least we have this issue here to prove that it is a known issue and we can reference it if needed. So I'd say it is up to you guys if you want to deal with the issue or not. Thanks