Poldz Kerk
Poldz Kerk

Reputation: 69

How to set and lock the JScrollpane to the bottom?

How can I set the JScrollPane to the bottom of the JTextArea? I tried the DefaultCaret caret = (DefaultCaret)getCaret(); caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); But it didnt work it didnt automatically go down.

Here is my current code

public class subTextAreaMessages extends JTextArea implements ActionListener{
private ArrayList al;
public subTextAreaMessages()
{
    setEditable(false);
    listTheMessages();
    Timer t = new Timer(500,this);
    t.start();
}

public void listTheMessages()
{
    ConnectMysql.getUsernamesTest(this);
}

@Override
public void actionPerformed(ActionEvent e) {

    try {
        System.out.print(getText(185,186));
    } catch (BadLocationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}
 }




jspTextField = new JScrollPane(new subTextAreaMessages());

mainPanel2.add(jspTextField ,BorderLayout.CENTER);

Upvotes: 0

Views: 560

Answers (2)

user3388920
user3388920

Reputation: 23

I had the problem that my text was being completely replaced periodically by similar content and users wanted to keep the visible location within the large text content fixed. My solution was to implement my own BoundedRangeModel by extending the DefaultBoundedRangeModel. Then I told the ScrollPane's ScrollBars to use an instance of my model and all went well.

Upvotes: 0

camickr
camickr

Reputation: 324118

Depending on your exact requirement, one of the following should help

  1. Text Area Scrolling
  2. Smart Scrolling

Upvotes: 1

Related Questions