|
103 | 103 | * } |
104 | 104 | * </pre> |
105 | 105 | * |
| 106 | + * <h3>Auto-Scrolling to the Caret</h3> |
| 107 | + * |
| 108 | + * <p>Every time the underlying {@link EditableStyledDocument} changes via user interaction (e.g. typing) through |
| 109 | + * the {@code StyledTextArea}, the area will scroll to insure the caret is kept in view. However, this does not |
| 110 | + * occur if changes are done programmatically. For example, let's say the area is displaying the bottom part |
| 111 | + * of the area's {@link EditableStyledDocument} and some code changes something in the top part of the document |
| 112 | + * that is not currently visible. If there is no call to {@link #requestFollowCaret()} at the end of that code, |
| 113 | + * the area will not auto-scroll to that section of the document. The change will occur, and the user will continue |
| 114 | + * to see the bottom part of the document as before. If such a call is there, then the area will scroll |
| 115 | + * to the top of the document and no longer display the bottom part of it.</p> |
| 116 | + * |
| 117 | + * <p>Additionally, when overriding the default user-interaction behavior, remember to include a call |
| 118 | + * to {@link #requestFollowCaret()}.</p> |
| 119 | + * |
106 | 120 | * <h3>Overriding keyboard shortcuts</h3> |
107 | 121 | * |
108 | 122 | * {@code StyledTextArea} uses {@code KEY_TYPED} handler to handle ordinary |
@@ -955,7 +969,12 @@ void showCaretAtTop() { |
955 | 969 | virtualFlow.showAtOffset(parIdx, -y); |
956 | 970 | } |
957 | 971 |
|
958 | | - void requestFollowCaret() { |
| 972 | + /** |
| 973 | + * If the caret is not visible within the area's view, the area will scroll so that caret |
| 974 | + * is visible in the next layout pass. Use this method when you wish to "follow the caret" |
| 975 | + * (i.e. auto-scroll to caret) after making a change (add/remove/modify area's segments). |
| 976 | + */ |
| 977 | + public void requestFollowCaret() { |
959 | 978 | followCaretRequested = true; |
960 | 979 | requestLayout(); |
961 | 980 | } |
|
0 commit comments