Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to force a scrollbar to scroll down by itself

Status
Not open for further replies.

Jimguld

Programmer
Mar 4, 2003
26
DK
Hi , I have a JTextarea that I add text to every time I press a button. But I can't seem to get the textarea to scroll down by itself so I can see the last added line.

I have tried going about the viewport, but it sucks a bit.
Any suggestions

Tried this..but not really good

JViewport port = (JViewport)scrollreply.getViewport();
port.scrollRectToVisible(port.getViewRect());
 
Okay fine, but how do I use it...which position do I provide
 
Using the crude code below the automatic scrolling works for me, even without calling setCaretPosition(). Is this what you need?


regards,

Barry


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

public class AutoScroll
{
    private static JTextArea textArea;

    public static void main(String [] args)
    {
        JButton addButton = new JButton("Add");        

        textArea = new JTextArea();
        JScrollPane pane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);        

        JFrame frame = new JFrame();
        frame.setSize(300,300);
        frame.setLocation(200,200);

        JPanel north = new JPanel();
        north.setLayout(new GridLayout());
        north.setPreferredSize(new Dimension(280,200));
        north.add(pane);

        JPanel south = new JPanel();
        south.setLayout(new GridLayout());
        south.add(addButton);

        Container c = frame.getContentPane();
        c.add(north,"North");
        c.add(south,"South");

        addButton.addActionListener(new ActionListener()
            {                
                public void actionPerformed(ActionEvent e)
                {
                    //Add some text
                    textArea.setText(textArea.getText() + "Adding some text \n");
                    //textArea.setCaretPosition(textArea.getText().length());
                }
            });

        frame.show();
    }
}
 
Thanks, using

textArea.setCaretPosition(textArea.getText().length());


this works works. It does not work without, strange I think....

Cheers
Jim

 
Hmmmmmmmm... Which SDK are you using? I have 1.3.1.08 and 1.4.0. It works fine for me on both versions without the setCaretPosition(). Also which OS? I am using Windows 2000.



Barry
 
I may be a bit out of time...I am using C:\jdk1.3.0_01

maybee this could be the problem.
 
I would say that is the problem. If you are forced to use that version, then you can just use the setCaretPosition()


Barry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top