Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

JScrollBar error



My App in Java 1.5 has a DefaultStyledDocument in a JScrollPane.
I keep adding more text to the Document and removing them when the
Document.length() > 20000.
(And by the way, if you ever do this, the Document saves all the deleted
stuff unless you override methods in GapContent, which I do-- otherwise
you eventually run out of memory.)

But I would like always to set the scroll bar to the bottom of the
Document after entering text. I do this using:
   private void updateLength() {

      try {
         int docLength = doc.getLength();

            if (docLength > 20000) {
                doc.remove(0, docLength - 20000);
            }
            // Set the cursor and text to the bottom
            docLength = doc.getLength();
            ep.setCaretPosition(docLength);
            int docPosition = doc.getEndPosition().getOffset();
            if(docPosition != docLength + 1) {
               System.err.println("Length=" + docLength
                  + " Caret Position=" + ep.getCaretPosition()
                  + " Document Position=" +
doc.getEndPosition().getOffset());
            }
             // For some reason this kills the Document.
            // Check that the user is not scrolling
            if(!(jsb.getModel().getValueIsAdjusting())) {
               int visibleAmount = jsb.getVisibleAmount();
               int maxSB = jsb.getMaximum();
               jsb.setValue(maxSB - visibleAmount);
            }
 
        }
      catch (BadLocationException ex) {
       
Logger.getLogger(MyTrapReceiver.class.getName()).log(Level.SEVERE, null,
ex);
      }
   }

This always eventually screws up my document and I get out of bounds
errors and also things like
Exception in thread "AWT-EventQueue-0"
java.lang.ArrayIndexOutOfBoundsException
        at java.lang.System.arraycopy(Native Method)
        at javax.swing.text.BoxView.updateLayoutArray(BoxView.java:197)
        at javax.swing.text.BoxView.replace(BoxView.java:173)
        at javax.swing.text.View.append(View.java:432)
        at javax.swing.text.FlowView$FlowStrategy.layout(FlowView.java:412)
        at javax.swing.text.FlowView.layout(FlowView.java:184)
        at javax.swing.text.BoxView.setSize(BoxView.java:379)
        at javax.swing.text.BoxView.updateChildSizes(BoxView.java:348)
        at javax.swing.text.BoxView.setSpanOnAxis(BoxView.java:330)
        at javax.swing.text.BoxView.layout(BoxView.java:682)
        at javax.swing.text.BoxView.setSize(BoxView.java:379)
        at
javax.swing.plaf.basic.BasicTextUI$RootView.setSize(BasicTextUI.java:1619)
        at
javax.swing.plaf.basic.BasicTextUI.getPreferredSize(BasicTextUI.java:813)
        at javax.swing.JComponent.getPreferredSize(JComponent.java:1624)
        at javax.swing.JEditorPane.getPreferredSize(JEditorPane.java:1227)
        at
javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:769)
        at java.awt.Container.layout(Container.java:1401)
        at java.awt.Container.doLayout(Container.java:1390)

It works if I leave out the jsb.setValue chunk of code. What am I doing
wrong?

Jim Rome
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.