File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Problem adjusting JScrollPane programatically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem adjusting JScrollPane programatically" Watch "Problem adjusting JScrollPane programatically" New topic

Problem adjusting JScrollPane programatically

Jeff Hinshaw

Joined: Mar 11, 2005
Posts: 21
I have a JScrollPane with a JTextArea. The JTextArea is loaded with 10 lines of data.

I have two JCheckBox's.

The first check box sets the scroll to 10.

The second check box reloads (ta.setText(text)) the text and then sets the scroll to 10.

Why does the second one not work?

I have attached a simple program. It is probably easier for you to compile and run than for me to go into detail explaining further.

Thanks in advance!

Peace,<br /> <br />Jeff
Sam Codean
Ranch Hand

Joined: Feb 26, 2006
Posts: 194
The Swing updates the UI related changes only once using the Event Dispatch Thread (EDT). The method

sp.getVerticalScrollBar().setValue(10); Does not update the UI. It is simply setting the value at 10. hence it is done immediately.
The second method that add the text to the screen willbring the Scroll Pane down but this will not be done immediately. It will be done on the EDT.
Hence when you run your code it will always run the setValue() method first and the setText() method later. Thus you will not see the effect of setValue().

The Solution would be to use SwingUtilities.invokeLater(). I have modified your code a little to make it more neater.
Hope it help!!!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Tester extends JFrame {

private static final String text = "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n";

private JPanel contentPane;
private Box box;
private JScrollPane sp;
private JTextArea ta;
private JCheckBox cb1;
private JCheckBox cb2;

public Tester() {

this.contentPane = (JPanel )this.getContentPane();


// this.setExtendedState(MAXIMIZED_BOTH);

// add listeners
this.addWindowListener(new FrameListener());
cb1.addItemListener(new Cb1Listener());
cb2.addItemListener(new Cb2Listener());

private void initComponents(){
ta = new JTextArea(text,5,2);
sp = new JScrollPane(ta,

box = Box.createVerticalBox();
cb1 = new JCheckBox("set scroll 10",false);
cb2 = new JCheckBox("reload txt area and set scroll 10",false);

private void placeComponents(){
contentPane.add(sp, BorderLayout.CENTER);
contentPane.add(box, BorderLayout.EAST);

private class FrameListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {

private class Cb1Listener implements ItemListener {
public void itemStateChanged(ItemEvent e) {


private class Cb2Listener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
final Runnable run2 = new Runnable(){
public void run(){

Runnable run1 = new Runnable(){
public void run(){



public static void main(String[] args) {
Tester a = new Tester();


-Sam Codean<br />SCJP 1.4 (98%)<br />SCJD 5.0 (87.5%)
Jeff Hinshaw

Joined: Mar 11, 2005
Posts: 21
Thanks, obviously that did the trick.

Thanks for the explanation as well.
Roedy Green

Joined: Feb 28, 2011
Posts: 2
To scroll back to the top, JscrollPane.getVerticalScrollBar.setValue does nothing. You must use TextComponent.setCaretPosition instead or JTable.scrollRectToVisible.
I agree. Here's the link:
subject: Problem adjusting JScrollPane programatically
It's not a secret anymore!