I have JEditorPane (communicationView) in my Swing application, and sometimes I need scroll it to the bottom. Scrolling is done by setting caret position to the document end.
private void setCommunicationViewCaretPosition(){
communicationView.setCaretPosition(communicationView.getDocument().getLength());
}
This usually works, but after a while following exception gets thrown:
Exception in thread "Thread-6" java.lang.IllegalStateException: cannot open system clipboard
at sun.awt.windows.WClipboard.openClipboard(Native Method)
at sun.awt.datatransfer.SunClipboard.getClipboardFormatsOpenClose(SunClipboard.java:332)
at sun.awt.datatransfer.SunClipboard.isDataFlavorAvailable(SunClipboard.java:192)
at org.jdesktop.application.TextActions.updateTextActions(TextActions.java:132)
at org.jdesktop.application.TextActions.access$400(TextActions.java:47)
at org.jdesktop.application.TextActions$TextComponentCaretListener.caretUpdate(TextActions.java:115)
at javax.swing.text.JTextComponent.fireCaretUpdate(JTextComponent.java:407)
at javax.swing.text.JTextComponent$MutableCaretEvent.fire(JTextComponent.java:4417)
at javax.swing.text.JTextComponent$MutableCaretEvent.stateChanged(JTextComponent.java:4439)
at javax.swing.text.DefaultCaret.fireStateChanged(DefaultCaret.java:802)
at javax.swing.text.DefaultCaret.changeCaretPosition(DefaultCaret.java:1277)
at javax.swing.text.DefaultCaret.handleSetDot(DefaultCaret.java:1173)
at javax.swing.text.DefaultCaret.setDot(DefaultCaret.java:1154)
at javax.swing.text.DefaultCaret.setDot(DefaultCaret.java:1051)
at javax.swing.text.JTextComponent.setCaretPosition(JTextComponent.java:1680)
at aau.application.ApplicationView.setCommunicationViewCaretPosition(ApplicationView.java:643)
I can't figure out why is this happening, or what setting caret position has to do with system clipboard.
Setting caret position seems to be the best way to scroll down the view but any other method would solve my problem.
Why? Are you scrolling because you just added new text and you want to see the text your just added, or do you have a "Scroll to Bottom" button.
In the first case you can check out Text Area Scrolling for a suggestion. I normally use a JTextArea or JTextPane to display text. A JEditorPane is used for HTML only so I'm not sure if this suggestion will work.
For the second case you can just use the vertical scrollbar and set it value to the scrollbar maximum value.