Using a custom Document implementation with JTextArea

Discussion in 'Java' started by Brian J. Sayatovic, Sep 7, 2003.

  1. 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.
     
    Brian J. Sayatovic, Sep 7, 2003
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Inertia_sublimation
    Replies:
    10
    Views:
    848
    Andrew Thompson
    Jan 1, 2004
  2. Tony Prichard
    Replies:
    0
    Views:
    1,005
    Tony Prichard
    Dec 12, 2003
  3. Michael Tsang
    Replies:
    32
    Views:
    1,554
    Richard Bos
    Mar 1, 2010
  4. Michael Tsang
    Replies:
    54
    Views:
    1,611
    Phil Carmody
    Mar 30, 2010
  5. sanket
    Replies:
    7
    Views:
    1,447
    Tsung
    Nov 3, 2011
  6. James Black
    Replies:
    10
    Views:
    445
    Thomas 'PointedEars' Lahn
    May 25, 2006
  7. SMH
    Replies:
    2
    Views:
    437
  8. icogs
    Replies:
    0
    Views:
    237
    icogs
    Jan 31, 2008
Loading...