tags:

views:

328

answers:

3

Hi, i'm begginner in java, i have textarea and i have set only verticle scrollbar to that textarea.i'm appending data for every 1 minute to textarea,problem is when new data appends to the textarea scrollbar will move up.To see the new data,every time i have to drag the scroll bar, that is not the requirment.i want scrollbar should not move up it should move down how can i do this? plz help me.

thanks for reply

+1  A: 

Try setting the caret location to the last position everytime you append:

textArea.setCaretLocation(textArea.getText().length());
Martijn
+1  A: 

Something like this should work:

JTextArea display= new JTextArea();
JScrollPane scroll =new JScrollPane(display);

scroll.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){
    public void adjustmentValueChanged(AdjustmentEvent e){
        JTextArea textArea = (JTextArea)e.getSource();
        textArea.setCaretPosition(textArea.getDocument().getLength()); 
    }
});

This way it will be fully automated.

Max
I imagine this will also fully restrict scrolling, which might not be desirable.
Martijn
+1  A: 

You can do

JTextArea textArea = new JTextArea();
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

and then use append as you currently do.

Barth
Thanks i got correct output for your second answer
Ok, I will update my answer to keep only the second part.
Barth