Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2003

Tip: Looking for answers? Try searching our database.

Using a custom Document implementation with JTextArea

Thread view: 
Brian J. Sayatovic - 07 Sep 2003 23:18 GMT
I just made my own, optimized Document implementation for use in a
JTextArea.  I optimized it to handle large amounnts of text in anappend-only
fashion.  However, when I put it to use, I get a NullPointerException deep
down inside PlainView:

java.lang.NullPointerException
   at javax.swing.text.PlainView.getLineWidth(Unknown Source)
   at javax.swing.text.PlainView.getPreferredSpan(Unknown Source)
   at javax.swing.plaf.basic.BasicTextUI$RootView.getPreferredSpan(Unknown
Source)
   at javax.swing.plaf.basic.BasicTextUI.getPreferredSize(Unknown Source)
   at javax.swing.JComponent.getPreferredSize(Unknown Source)
   at javax.swing.JTextArea.getPreferredSize(Unknown Source)
   at
javax.swing.text.JTextComponent.getPreferredScrollableViewportSize(Unknown
Source)
   at javax.swing.JTextArea.getPreferredScrollableViewportSize(Unknown
Source)
   at javax.swing.ViewportLayout.preferredLayoutSize(Unknown Source)
   at java.awt.Container.preferredSize(Unknown Source)
   at java.awt.Container.getPreferredSize(Unknown Source)
   at javax.swing.JComponent.getPreferredSize(Unknown Source)
   at javax.swing.ScrollPaneLayout.preferredLayoutSize(Unknown Source)
   at java.awt.Container.preferredSize(Unknown Source)
   at java.awt.Container.getPreferredSize(Unknown Source)
   at javax.swing.JComponent.getPreferredSize(Unknown Source)
   ...

The problem boils down to the fact that my Document implementation initially
has no content whatsoever.  Thus, the root Element, which represents the
entire Document, has no child elements.  This leaves the longestLine member
inside PlainView as null, so any call utilizing it, including
getLineWidth(), will throw a NullPointerException.

Now, I read that the default Documents utilize a scheme where their Content
always has an extra character at the end to allow for the caret to be placed
at the end of the real content.  I imagine this means that even an empty
PlainDocument, for example, always has at least one element representing
that single newline character used in practice.

Is it necessary to do this in my case, or is there an easy way to subclass
PlainView and force my JTextArea to use this subclass instead?

Regards,
Brian.
Alan Moore - 08 Sep 2003 05:02 GMT
>I just made my own, optimized Document implementation for use in a
>JTextArea.  I optimized it to handle large amounnts of text in an append-only
>fashion.  However, when I put it to use, I get a NullPointerException deep
>down inside PlainView:

<stacktrace snipped>

>The problem boils down to the fact that my Document implementation initially
>has no content whatsoever.  Thus, the root Element, which represents the
[quoted text clipped - 13 lines]
>Regards,
>Brian.

I recommend you do like Sun and use a placeholder; getLineWidth() is
not the only method that will throw an NPE, just the first.  In fact,
the whole javax.swing.text package seems to be based on an unwritten
contract requiring a document to contain at least one non-null leaf
element at all times.

--Alan
Brian J. Sayatovic - 08 Sep 2003 14:15 GMT
Is it enough to have a non-null leaf?  In other words, can I have one
leaf element with a start/end offset of 0?  The JavaDocs for Element
indicate that both start and end offsets can be >= 0, so it seems like
it would be legal.  This would allow be to have a non-null element,
and I believe that would make the longestLine essentially be an empty
String with 0 length.  Perhaps that would cause other problems, but is
it worth trying?

Heck, I'll try it tonight anyways unless I hear otherwise.

Regards,
Brian.

> I recommend you do like Sun and use a placeholder; getLineWidth() is
> not the only method that will throw an NPE, just the first.  In fact,
[quoted text clipped - 3 lines]
>
> --Alan


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.