• Post Reply Bookmark Topic Watch Topic
  • New Topic

Automatic scrolling when inserting text in JTextArea

 
Laurent ROCHE
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a little application using a window to log a few operations.
In the window, there is a JTextArea inserted in a JScrollPane.
When I insert the text in the JTextArea as soon as the bottom of the area is reached then there is no scrolling and I can not see the text being inserted.
I would like to see the area scrolling when the text is inserted, I have tried a few things, looked in a few places and did not find any solutions.
The best I could come up with is the following:
void addText(String s) {
_logText.append(s);
_logText.getCaret().setDot( _logText.getText().length() );
_pane.scrollRectToVisible(_logText.getVisibleRect() );
this.paint(this.getGraphics());
}
where _logText is the JTextArea and _pane is the JScrollPane.
Any ideas, any one ?
------------------
A froggy in London
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Laurent,
The easiest way is to put the text area in the scroll pane and add it to the frame or any parent....Here's an example for your perusal:
import javax.swing.*;
public class Scroller extends JFrame
{JTextArea ta;
JScrollPane scPane;
public Scroller()
{super("Example Scroller");
createGUI();
// add a window closing event here!
}
private void createGUI()
{ta = new JTextArea();
scPane = new JScrollPane(ta);
getContentPane().add(scPane);
setSize(400,400);
setVisible(true);
}
public static void main(String[] args)
{new Scroller();
}
}
...try this out....
hope this helps!
Have fun,
~Kavitha
 
Laurent ROCHE
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, there is a communication problem here !
I did not mean I could not display the JTextArea whithin a JScrollPane.
Obviously, I did what you described.
Once again, the problem is that when the program adds lines in the JTextArea and the bottom of the JTextArea is reached, then the text is not scrolled automatically, and I have to manually use the scrollbar to see the rest of the text.
And again, I have seen this problem in many places, I can not believe there is no solution for it.
I forgot to mention in the original post that my solution only scroll to the end ONCE ALL THE TEXT HAS BEEN INSERTED.
That means it is not satisfactory as when the text is inserted, one can not see it.
------------------
A froggy in London
[This message has been edited by Laurent ROCHE (edited July 06, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi
I had the same problem.
I had a JTextArea within a JScrollPane.
I had a single thread running which invoked
(say every 10 seconds) a method which will do
a textArea1.append("some text\n").
The scroll wont show me the last line getting appended.
If the append() is done without any threading, it was fine.
Scrolling was perfect.
I came to a conclusion that it has something to do with "Swing" not being "Threadsafe".
I did not have time to do more research. So I simply replaced the Swing - JTextArea with AWT - TextArea, which has inbuilt scrolling capablity.
Now the scrolling is perfect, it shows the latest line added.
Hope this helps. Let me know if it worked.
- Srini.


 
Laurent ROCHE
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Srini: that worked.
From what I have read around, I had the feeling that might be a solution, but I was not really happy about it.
Your post decided me to test it: just changing the JTextArea to TextArea did it. I did not need to change any thing else.
If I get some time, I will get a book about SWING and try to understand why this does not work.
Thanks for your help anyway,
L.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!