DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2004
    Posts
    9

    JTextArea Scrolling..?

    Okay, this should be an easy one to answer, but I just can't figure it out...
    I have a JTextArea that I am appending text to. I put it inside a JScrollPane so that I can get a scroll bar. The problem is that when I get enough text in the JTextArea for the scrollbar to appear, I would like the visible area of the JTextArea to display the new text that I just appended, and shift off the top line of the old text off.

    Sumarizing...all I want is for the JTextArea to automatically scroll down as I add more text.

    Thanks!

    Code Snippet:

    ////////////Declarations///////////////
    JTextArea statusBox = new JTextArea("Welcome!"+'\n');
    JScrollPane scrollBox = new JScrollPane(statusBox);


    /////////Code////////////////////
    String text = "....";
    statusBox.append(text + '\n');

  2. #2
    Join Date
    Sep 2004
    Posts
    223
    you will need to set the cursor position, maybe this will work:

    Code:
    myJTextArea.setCaretPosition(myJTextArea.getDocument().getLength());
    A kram a day keeps the doctor......guessing

  3. #3
    Join Date
    Nov 2004
    Posts
    3
    import javax.swing.*;
    import java.awt.event.*;


    public class ScrollGUI implements ActionListener {

    private JFrame frame;
    private JTextArea text;
    private JPanel panel;
    private JButton button;

    public static void main(String[] args) {
    new ScrollGUI();
    }

    public ScrollGUI() {

    frame = new JFrame("--ScrollBar---");
    text = new JTextArea(15, 33);
    JScrollPane scroll = new JScrollPane(text);
    panel = new JPanel();
    button = new JButton("Add some text");
    button.addActionListener(this);
    panel.add(button);
    panel.add(scroll);
    frame.getContentPane().add(panel);
    frame.setSize(415, 400);
    frame.setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {

    text.append("\n\n\nSome text \n\n to\n\n append");

    }
    }




    This simple example always scrolls to the bottom....
    but when i tried the same thing with some initial text in the textarea the scrollbar stayed at the top. hmmm.

  4. #4
    Join Date
    Oct 2004
    Posts
    9
    I tried removing my initial text, but it still didn't scroll.

    I added your code Kram, and it worked. Thanks!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links