-8

I have a Swing application with Nimbus l&f. Sometime I got this exception but I don't know where it is triggered and how to avoid it because all sources are from Java library classes in the trace stack. Is this a Java issue or my issue?

Some info about my system:

avaVersion = 1.8.0_20

osName = Linux

osVersion = 3.15.10-201.fc20.x86_64

Exception message:

java.lang.ClassCastException: java.awt.Font cannot be cast to javax.swing.Painter
        javax.swing.plaf.nimbus.NimbusStyle.getBackgroundPainter(NimbusStyle.java:708)
        javax.swing.plaf.nimbus.SynthPainterImpl.paintBackground(SynthPainterImpl.java:99)
        javax.swing.plaf.nimbus.SynthPainterImpl.paintPanelBackground(SynthPainterImpl.java:957)
        javax.swing.plaf.synth.SynthPanelUI.update(SynthPanelUI.java:154)
        javax.swing.JComponent.paintComponent(JComponent.java:777)
        javax.swing.JComponent.paint(JComponent.java:1053)
        javax.swing.JComponent.paintToOffscreen(JComponent.java:5223)
        javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:290)
        javax.swing.RepaintManager.paint(RepaintManager.java:1265)
        javax.swing.JComponent._paintImmediately(JComponent.java:5171)
        javax.swing.JComponent.paintImmediately(JComponent.java:4982)
        javax.swing.RepaintManager$4.run(RepaintManager.java:824)
        javax.swing.RepaintManager$4.run(RepaintManager.java:807)
        java.security.AccessController.doPrivileged(Native Method)
        java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
        javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:807)
        javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:782)
        javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:731)
        javax.swing.RepaintManager.access$1300(RepaintManager.java:64)
        javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1720)
        java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
        java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
        java.awt.EventQueue.access$400(EventQueue.java:97)
        java.awt.EventQueue$3.run(EventQueue.java:697)
        java.awt.EventQueue$3.run(EventQueue.java:691)
        java.security.AccessController.doPrivileged(Native Method)
        java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
        java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
        java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
        java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
java.awt.Font cannot be cast to javax.swing.Painter

EDIT:

If it is consistency and happen very often, I may be able to figure it out.

It is also impossible for me to try to cast a Font to Painter obviously.

EDIT2:

The full exception stack is copied here.

EDIT3

The application GUI part is started within SwingUtilities.invokeAndWait().

I also use my Win7 machine a lot before but never get this on Win7. This application already exists 1 year.

peterboston
  • 877
  • 1
  • 12
  • 24
  • 3
    You can avoid this exception if you do not try casting a `Font` to a `Painter`. – Sergey Kalinichenko Dec 24 '15 at 16:46
  • You have an intermittent error which highly suggests a concurrency or threading issue. Are you starting the GUI on the Swing event thread? Be sure to do this, especially with some Look & Feels such as Nimbus. In other words -- create your GUI within a Runnable and queue that onto the Swing event queue using `SwingUtilities.invokeLater(...)`. – Hovercraft Full Of Eels Dec 24 '15 at 16:52
  • 1
    You have to give us your code... Otherwise we can't recreate the problem and can't really help – Raven Dec 24 '15 at 16:56
  • 1
    Just a guess here, but are you possibly passing some Font Object that later Nimbus expects to be a Painter Object. I would suggest trying to debug this problem by monitoring any Font Objects you create and use, to see if that is the culprit. Depending on the size of your project this could easily help you deduct the problem or may not be feasible... Just some thoughts. Hope it helps. – DavidR Dec 24 '15 at 17:00
  • I agree with @sstan: please change all-caps to standard English usage. Please post pertinent code, especially how and where you're setting your Look & Feel. – Hovercraft Full Of Eels Dec 24 '15 at 17:01
  • I don't understand how this is getting so many down votes! This is clearly something weird going on. He isn't just trying to cast a Font to a Painter, Nimbus is. I think this is a legit question. – DavidR Dec 24 '15 at 17:02
  • @DavidR: myself, I didn't down-vote til the shouting began, and til despite the edits from the OP and despite our requests -- no pertinent code. – Hovercraft Full Of Eels Dec 24 '15 at 17:03
  • 1
    @HovercraftFullOfEels - Yes, some code would help. – DavidR Dec 24 '15 at 17:05
  • Please see edit to community wiki. I am now fully convinced that you have code that violates Swing threading rules. That, and that your declining to show pertinent code is hamstringing our efforts to help, pure and simple. For more on this check [this Google search](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=java%20nimbus%20classcastexception). – Hovercraft Full Of Eels Dec 24 '15 at 19:38

1 Answers1

4

You have an intermittent exception being thrown from a Swing GUI which highly suggests that this is a concurrency/threading issue. Are you starting the GUI on the Swing event dispatch thread? If not, please be sure to do this, especially with some Look & Feels such as Nimbus. In other words -- create your GUI within a Runnable and queue that onto the Swing event queue using SwingUtilities.invokeLater(...).

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        // create your Swing GUI and set the L&F here
    });
}

If this is not the problem, then still look for other Swing threading issues, possibly using one of the approaches cited here.


Please see this bug report on the same issue. It was closed as a "non-issue" because:

This is most definitely a multithreading issue.

Again, I can now say with confidence that yours is a threading issue, that the way to solve it is to go through your code and find out where your code is violating Swing's threading rules, because most certainly it is. If you need our help, then you must show us your pertinent code, that is, code that might be violating Swing threading rules. If you won't show code, we can't give specific help.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Swing only have one EDT. if ALL GUI related actions are constrained within EDT, that will eliminate this issue? – peterboston Dec 25 '15 at 14:26
  • @peterboston: all GUI actions should be called on the EDT period. All long-running actions should be called in background threads. The way to find out if this fixes the problem is to test it. – Hovercraft Full Of Eels Dec 25 '15 at 14:29