Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Ludwig Fischer   on Jun 02 In Java Category.

  
Question Answered By: Viheke Fischer   on Jun 02

The following is some demo code that does what you need.
After you append text to your JTextArea, set the CaretPosition to the length of
all the text in the JTExtArea and it will scroll down to the end and you go.
I put it in its own private method  to make it convenient:

private void appendLine(String text){
display.append(text + "\n");
display.setCaretPosition(display.getText().length());
}


********************
import java.awt.*;
import javax.swing.*;

public class TestDisplayScroller extends JFrame{

JTextArea display;

public TestDisplayScroller(){
super("TestDisplayScroller");
}

public static void main (String[] args){
TestDisplayScroller scroller = new TestDisplayScroller();
System.out.println("Test");
scroller.init();
scroller.test();
}

public void test(){
for (int i = 0; i < 100; i++){
appendLine("this is a test to see if the display scrolls down as new
lines are added.");
}
}


public void init(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
display = new JTextArea(20,80);
JScrollPane scrollPane = new JScrollPane(display);
getContentPane().add(scrollPane, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
show();
}

private void append(String text){
display.append(text);
display.setCaretPosition(display.getText().length());
}

private void appendLine(String text){
display.append(text + "\n");
display.setCaretPosition(display.getText().length());
}
}

Share: 

 

This Question has 20 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on jframe not moveable Or get search suggestion and latest updates.


Tagged: