0

We've been struggling here at the office with a seemingly unsolvable bug in one of our (massive) applets, this one being a Java simulation of MS Word, for exam purposes. The exception seems to stem from swing itself, and I'm not sure how I would go about solving it. Here's what happens:

The applet loads with a loading screen till 100%. After this, the components are all displayed in a broken way as if some of their paint methods are not being called correctly, hovering the cursor over some buttons etc. does make them flash/display for a while. And an exception is printed to the console every second or so. (See below) This bug does NOT occur within Eclipse, only in the browser.

Summary: Applet components all get built without exceptions, but on every "paint" call of a some component (I assume), it visually bugs out and the following is printed every second:

Exception in thread "AWT-EventQueue-11" java.lang.ClassCastException: javax.swing.JLabel cannot be cast to javax.swing.text.JTextComponent
 at javax.swing.text.html.EditableView.paint(Unknown Source)
 at javax.swing.text.BoxView.paintChild(Unknown Source)
 at javax.swing.text.BoxView.paint(Unknown Source)
 at javax.swing.text.BoxView.paintChild(Unknown Source)
 at javax.swing.text.BoxView.paint(Unknown Source)
 at javax.swing.text.ParagraphView.paint(Unknown Source)
 at javax.swing.text.html.ParagraphView.paint(Unknown Source)
 at javax.swing.text.BoxView.paintChild(Unknown Source)
 at javax.swing.text.BoxView.paint(Unknown Source)
 at javax.swing.text.html.BlockView.paint(Unknown Source)
 at javax.swing.text.BoxView.paintChild(Unknown Source)
 at javax.swing.text.BoxView.paint(Unknown Source)
 at javax.swing.text.html.BlockView.paint(Unknown Source)
 at javax.swing.plaf.basic.BasicHTML$Renderer.paint(Unknown Source)
 at javax.swing.plaf.basic.BasicLabelUI.paint(Unknown Source)
 at javax.swing.plaf.ComponentUI.update(Unknown Source)
 at javax.swing.JComponent.paintComponent(Unknown Source)
 at javax.swing.JComponent.paint(Unknown Source)
 at javax.swing.JComponent.paintChildren(Unknown Source)
 at javax.swing.JComponent.paint(Unknown Source)
 at javax.swing.JComponent.paintChildren(Unknown Source)
 at javax.swing.JComponent.paint(Unknown Source)
 at javax.swing.JComponent.paintChildren(Unknown Source)
 at javax.swing.JComponent.paint(Unknown Source)
 at javax.swing.JComponent.paintChildren(Unknown Source)
 at javax.swing.JComponent.paint(Unknown Source)
 at javax.swing.JLayeredPane.paint(Unknown Source)
 at javax.swing.JComponent.paintToOffscreen(Unknown Source)
 at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
 at javax.swing.RepaintManager.paint(Unknown Source)
 at javax.swing.JComponent._paintImmediately(Unknown Source)
 at javax.swing.JComponent.paintImmediately(Unknown Source)
 at javax.swing.RepaintManager$3.run(Unknown Source)
 at javax.swing.RepaintManager$3.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
 at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
 at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
 at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
 at javax.swing.RepaintManager.access$1000(Unknown Source)
 at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
 at java.awt.event.InvocationEvent.dispatch(Unknown Source)
 at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
 at java.awt.EventQueue.access$200(Unknown Source)
 at java.awt.EventQueue$3.run(Unknown Source)
 at java.awt.EventQueue$3.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
 at java.awt.EventQueue.dispatchEvent(Unknown Source)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.run(Unknown Source)

Help would be greatly appreciated, as this is driving me nuts, I can't seem to find what triggers this, as the code is from a previous developer, and an utter mess. I have tried different JDK versions.

Thanx.

Mammon_ZA
  • 1
  • 2
  • 1
    Looking at [the code](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/swing/text/html/EditableView.java#EditableView.paint%28java.awt.Graphics%2Cjava.awt.Shape%29) for the method that threw, it appears that you somehow have a `JLabel` acting as the container for a text component. I would start by looking for calls to `add()` on any of your labels. – parsifal Jun 25 '13 at 14:26
  • Good idea, looking for that now. – Mammon_ZA Jun 25 '13 at 14:30
  • Remove all things out of the module, leave bare bones applet, then start adding piece by piece till you find the place in code which generates error. –  Jun 25 '13 at 14:18
  • With a large and badly coded applet such as this, this is a last resort for me. But yeah I'm currently trying to narrow down the trigger. – Mammon_ZA Jun 25 '13 at 14:28

3 Answers3

4

java.lang.ClassCastException: javax.swing.JLabel cannot be cast to javax.swing.text.JTextComponent

  • see components tree for JTextComponents, there isn't JLabel, JLabel isn't member of JTextComponents

  • safest could be to test if (Xxx instanceof JTextComponent) before any casting

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Except that the cast is happening deep within Swing internal code. So the real question (and real answer) is how a `JLabel` ended up as a child of a `BoxView`. – parsifal Jun 25 '13 at 14:20
  • That's the weird part. Nowhere in my code is there a cast made to any subcomponent of, or to JTextComponent. Wouldn't the stack trace then point me to my source instead of to within swing? – Mammon_ZA Jun 25 '13 at 14:23
  • 2
    [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) applies to applets, too. – trashgod Jun 25 '13 at 14:59
  • 1
    @DavidKroukamp: It might explain the environmental dependence. Mammon: see also these [tracking tools](http://stackoverflow.com/q/7787998/230513), cited by mKorbel. – trashgod Jun 25 '13 at 15:34
0

Found a fix to my problem. The target Java version was wrong. In the build.xml file, there were the following lines:

<property name="target" value="1.5" />
<property name="source" value="1.5" />

Changed those to 1.7, and voila! This seems to have been a Swing bug that has been fixed in the newer versions of Java.

Mammon_ZA
  • 1
  • 2
0

I've noticed that this problem can occur when a JLabel with tags around the content contains nested tags. The solution is to escape the angle brackets on the

Jesse Barnum
  • 6,507
  • 6
  • 40
  • 69