I am trying to implement a JScrollPane with a JTextArea. The JTextArea is being appended to, and I want the JScrollPane to keep scrolling down as more text is added. How can this be achieved?
Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.
Just use the reference to your JScrollPane object, get the vertical scroll bar from it using getVerticalScrollBar , and then call setUnitIncrement on it, like this: myJScrollPane. getVerticalScrollBar().
A JSrollPane is used to make a scrollable view of a component. A scroll pane is an object of the JScrollPane class which extends JComponent class. When screen size is limited, we use a scroll pane to display a large component or a component whose size can change dynamically.
For (what I think is) a simpler answer check out: Text Area Scrolling.
Prior to JDK5, you would have to manually change the caret's position after each append. You can now give this behaviour as a default like this :
JTextArea textArea = new JTextArea(); DefaultCaret caret = (DefaultCaret)textArea.getCaret(); caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
The advantage of this is that you don't need to use this snippet more than once in your code!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With