Scroll action in java

I am a beggar in java, I have a textarea and I have only set the vertical scrollbar to this file. I add data every 1 minute to the textarea, the problem is when new data is attached to the textarea scrollbar, it will move up. To see new data, every time I have to drag the scrollbar, this is not a requirement. I need the scrollbar not to move up, it should move down, how can I do that? Plz help me.

thanks for the answer

0


source to share


3 answers


Try adjusting the caret location to the last position every time you add:



textArea.setCaretLocation(textArea.getText().length());

      

+1


source


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()); 
    }
});

      



Thus, it will be fully automated.

+1


source


You can do

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

      

and then use append as you are currently doing.

+1


source







All Articles