Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll JScrollPane to bottom

Tags:

java

swing

I need to scroll a JScrollPane to the bottom. The JScrollPane contains a JPanel, which contains a number of JLabel's.

To scroll to the top, I just do:

scrollPane.getViewport().setViewPosition(new Point(0,0)); 

but how do I scroll exactly to the very bottom? (Too far and it jitters)

like image 703
Matt Avatar asked Feb 28 '11 21:02

Matt


People also ask

What is the difference between JScrollPane and JScrollBar?

A JScrollBar is a component and it doesn't handle its own events whereas a JScrollPane is a Container and it handles its own events and performs its own scrolling.

How do I make my JScrollPane scroll faster?

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(). setUnitIncrement(16);

How do I add a scroll pane to a JPanel?

getContentPane(). add(scrollPanel); This code will work in general to add JScrollPane to JPanel. Adjust bounds of frame, panel and scrollpane according to your requirements but ensure that the bounds of JScrollPane are within the bounds of the frame otherwise the scrollpane will not be visible.


2 Answers

JScrollBar vertical = scrollPane.getVerticalScrollBar(); vertical.setValue( vertical.getMaximum() ); 
like image 187
camickr Avatar answered Sep 17 '22 13:09

camickr


After many hours of attempting to find an answer other than one using the scrollRectToVisible() method, I've succeeded. I've found that if you use the following code after you output text to the text area in the scrollpane, it will automatically focus on the bottom of the text area.

textArea.setCaretPosition(textArea.getDocument().getLength()); 

So, at least for me, my print method looks like this

public void printMessage(String message) {     textArea.append(message + endL);     textArea.setCaretPosition(textArea.getDocument().getLength()); } 
like image 21
Decesus Avatar answered Sep 21 '22 13:09

Decesus