ArrayIndexOutOfBoundsException when setting visibility to the caret

Hi all,

I made a JTextPane and run into some problems. I get an ArrayIndexOutOfBoundsException when I'm setting it's caret visibility to true and false:

JEditorPane textPane =new JTextPane;

// ....

// add the textPane to a frame's ContentPane

// ....

publicvoid test(){

for(int i = 0; i<100; i++){

testVisibility(true);

testVisibility(false);

}

}

privatevoid testVisibility(boolean b){

// This is were the exception will be thrown if the text pane is visible

// when making these calls.

textPane.setEditable(b);

textPane.getCaret().setVisible(b);

}

The snippet throws an ArrayIndexOutOfBoundsException:

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:168)

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:1618)

at javax.swing.plaf.basic.BasicTextUI.modelToView(BasicTextUI.java:946)

at javax.swing.text.DefaultCaret.setVisible(DefaultCaret.java:952)

at org.seba.JTextPaneTest.testVisibility(JTextPaneTest.java:38)

at org.seba.JTextPaneTest.test(JTextPaneTest.java:29)

at org.seba.JTextPaneTest.main(JTextPaneTest.java:24)

[2526 byte] By [textpanea] at [2007-11-27 6:06:37]
# 1

I created a minimal Swing app a JEditorPane / JTextPane just as you described, and subjected it to the same routine that you described, but I could not reproduce the error, which suggests to me that something else must be going on to cause the error.

Also the error message is intriguing as it shows messages referable to BoxView multiple times. Are you setting the content type of the editor pane or in some other way manipulating it that is not mentioned above? You need to create an [url=http://homepage1.nifty.com/algafield/sscce.html]SSCCE[/url] that reproduces your error.

petes1234a at 2007-7-12 16:22:28 > top of Java-index,Desktop,Core GUI APIs...
# 2
Can't reproduce the problem.Why do you setVisible to false on the caret? Doesn't it disappear when you setVisible to false on the textpane?
rebola at 2007-7-12 16:22:28 > top of Java-index,Desktop,Core GUI APIs...
# 3

import javax.swing.JEditorPane;

import javax.swing.JFrame;

import javax.swing.JTextPane;

public class JTextPaneTest {

JFrame mainFrame;

JEditorPane textPanel;

public JTextPaneTest() {

textPanel = new JTextPane();

mainFrame = new JFrame("Test Frame.");

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mainFrame.setSize(640, 480);

mainFrame.getContentPane().add(textPanel);

mainFrame.setVisible(true);

}

public static void main(String[] args) {

JTextPaneTest textPaneTest = new JTextPaneTest();

textPaneTest.test();

}

public void test() {

for(int i = 0; i<100; i++) {

testVisibility(false);

testVisibility(true);

}

}

private void testVisibility(boolean b) {

// This is were the exception will be thrown if the text panel is visible

// when making these calls.

textPanel.setEditable(b);

textPanel.getCaret().setVisible(b);

}

}

I hope it's ok. You'll have to use Java 5 (1.5.0.11) to compile and run it.

I run into the problem when I made the text pane not editable and the caret was still visible. I needed the caret to hide away too when making the text pane not editable. If I use the setEnabled(boolean) to disable the text pane, all the text is made grey.(I know that there are some methods to make this not happen). I just don't know why this simple test is not working as I would expect.

textpanea at 2007-7-12 16:22:28 > top of Java-index,Desktop,Core GUI APIs...
# 4
Your app works fine for me. No exceptions called.
petes1234a at 2007-7-12 16:22:28 > top of Java-index,Desktop,Core GUI APIs...
# 5

Increase the counter to 100.000 and please wait for the exception. I have tested it on some machine without HyperThreading / Dual Core and it did happen only after 1-2 minutes (around 55.000 iterations).

Update:

It happens frequently with the HT enabled(or the Dual Core enabled), it didn't happened at all when I've disabled the HT (I've run it two times with the counter set to 100.000) and it happened rarely (once with the counter set to 100.000) with the Dual Core disabled.

textpanea at 2007-7-12 16:22:28 > top of Java-index,Desktop,Core GUI APIs...