• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextPane exceptions  RSS feed

 
John Coleman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I add a component to a JTextPane, then the next time I use insertString, an exception is thrown. This sometimes disrupts event dispatcher so that multiple mouse clicks occur and making the whole GUI unstable - although not on both PCs I run the software on?

I've tried emulating the sequence of code in the Sun TextSamplerDemo example, and that works fine. All my GUI updates are in Runnable classes using invokeLater(). Perhaps this is a thread/concurrency issue?

The StyledDocument has a number of predefined text styles, but as the buttons are all customized, I add styles for them on the fly to route to the required action event.

code:

Style s = doc.addStyle(newstylename, doc.getStyle("regular"));
StyleConstants.setAlignment(s, StyleConstants.ALIGN_LEFT);
JButton replay = new JButton(cueaudio1_i);
replay.setToolTipText("play " + recfilename);
replay.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
replay.setHorizontalTextPosition(SwingConstants.LEADING);
replay.setMargin(new Insets(0,0,0,0));
replay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateStatus("queued play of " + recfilename);
new ReplayQueue(frame, recfilename);
}
});
StyleConstants.setComponent(s, replay);
doc.insertString(doc.getLength(), " \n", s);

Any help much appreciated as this is holding my project up.

John



java.lang.ArrayIndexOutOfBoundsException: No such child: 0
at java.awt.Container.getComponent(Container.java:237)
at javax.swing.text.ComponentView$Invalidator.cacheChildSizes(ComponentView.java:399)
at javax.swing.text.ComponentView$Invalidator.doLayout(ComponentView.java:383)
at java.awt.Container.validateTree(Container.java:1092)
at java.awt.Container.validate(Container.java:1067)
at javax.swing.text.ComponentView$Invalidator.validateIfNecessary(ComponentView.java:394)
at javax.swing.text.ComponentView$Invalidator.getAlignmentY(ComponentView.java:442)
at javax.swing.text.ComponentView.getAlignment(ComponentView.java:203)
at javax.swing.text.BoxView.baselineRequirements(BoxView.java:1000)
at javax.swing.text.ParagraphView$Row.calculateMinorAxisRequirements(ParagraphView.java:843)
at javax.swing.text.BoxView.checkRequests(BoxView.java:913)
at javax.swing.text.BoxView.getMinimumSpan(BoxView.java:542)
at javax.swing.text.BoxView.calculateMajorAxisRequirements(BoxView.java:849)
at javax.swing.text.BoxView.checkRequests(BoxView.java:908)
at javax.swing.text.BoxView.getMinimumSpan(BoxView.java:542)
at javax.swing.text.BoxView.calculateMajorAxisRequirements(BoxView.java:849)
at javax.swing.text.BoxView.checkRequests(BoxView.java:908)
at javax.swing.text.BoxView.getMinimumSpan(BoxView.java:542)
at javax.swing.plaf.basic.BasicTextUI$RootView.getMinimumSpan(BasicTextUI.java:1254)
at javax.swing.plaf.basic.BasicTextUI.getMinimumSize(BasicTextUI.java:835)
at javax.swing.JEditorPane.getScrollableTracksViewportWidth(JEditorPane.java:1343)
at javax.swing.JEditorPane.getPreferredSize(JEditorPane.java:1218)
at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:769)
at java.awt.Container.layout(Container.java:1020)
at java.awt.Container.doLayout(Container.java:1010)
at java.awt.Container.validateTree(Container.java:1092)
at java.awt.Container.validate(Container.java:1067)
at javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.java:353)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:116)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why are you adding components to a text pane ?
 
Jared Cope
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am looking into the same problem at the moment. I am adding a JTable to my JTextPane (building the JTable from values after parsing some XML).

If I hit the delete key a couple of times in the JTextPane just above where the JTable is rendered, then I get a similar stack dump as to what you posted.

Post back if you figure out what is going wrong -- I'll do the same.

Cheers, Jared.
 
John Coleman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, checked at Sun and there is a bug report for this.

Unfortunately the fix is to use java 1.5. Anyway, I can't confirm this as my old JBuilder doesn't support 1.5 builds.

There does not appear to be a workaround for 1.4 developers - I tried a few of the old tricks for JTExtPane issues with older JDKs, no good.

Guess I will just have to remove this feature from my app, which is pants!

John
[ February 11, 2005: Message edited by: John Coleman ]
 
John Coleman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Don Kiddick:
why are you adding components to a text pane ?


So that my text pane can include buttons to trigger replay of an audio file. But could include images or anything else. Basically JTextPane can behave like a browser, that's why you use it.

John
 
John Coleman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay def a JDK1.4 problem. After downloading JDK1.5 and then JBuilder2005 Foundation, I can build the project and run the JTextPane without the issues.

JDK1.5 Throws my serializations out though.

John
 
John Coleman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the story continues...

I went back to using JDK1.4.2 and am now able to insert JLabels okay so long as the inserted component is immediately followed by a new line.

i.e.
doc.insertString(doc.getLength(), " \n", doc.getStyle("regular"));

Anyway, does the trick. (setting the style is a must BTW)

John
[ February 12, 2005: Message edited by: John Coleman ]
 
Jared Cope
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the info. I will try out some of your suggestions here.

However, I think I may also try over-ridding the insertString() and remove() methods for the Document object of my JTextPane. I think a solution for myself may be to disable the ability to add or remove text immediately around the JTable that appears on my JTextPane. I hope this will stop the stack dump.

Of course I will have to provide another way of removing the JTable from the JTextPane. Perhaps a rightmouse click with a popup menu and remove option when clicked over the table.

Will let you know if I have any success with this.

Cheers, Jared.
 
John Coleman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can imagine this working okay, but make sure the JTable has some newline padding under it styled as I suggested before.

Although my "workaround" stops exceptions, I am still finding that the event dispatcher gets messed up, and the positioning is incorrect.

My verdict at present is that JTextPane is too messed up in 1.4 to be worth using with components, or at least be prepared to compromise. With the 1.5 JDK, everything works as expected. Expecting all your Java users to up to 1.5 just for this is a bit of a tall order IMO. These bugs should have been resolved much earlier on in the JDK development.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!