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);
}
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?
Does this work in other Look and Feels, like Metal, or GTK? the stack
trace seems to indicate this is failing in the "Basic" implementation
provided by Sun that most Look and Feels are based on.
I'm not sure why the text document model is running over the edge of
it's array storage, but I'm guessing it might just be a math bug.
Thanks,
Mike Swingler
Java Frameworks Engineer
Apple Inc.
_______________________________________________
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