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');
10-31-2004, 10:58 PM
Kram
you will need to set the cursor position, maybe this will work:
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.
11-01-2004, 12:20 PM
Mathias_B
I tried removing my initial text, but it still didn't scroll.