wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes JScrollpane - Force autoscroll to bottom Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply locked New topic
Author

JScrollpane - Force autoscroll to bottom

Mike Morse
Greenhorn

Joined: Oct 31, 2000
Posts: 3
Hi
I am currently using JScollpane with a JTextArea.
The JTextarea is for incoming messages in a chatroom panel.
Currently when a new message is added to the JTextarea the text is added to the next row but the scrollbar is NOT scrolling to the next row. What is the best way to force the scrollpane to scroll to the next row or bottom of JTextarea?
Thank You all for your help.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I didn't have time to check it, but I think if you select() the last character in the JTextArea it will scroll to that line.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Mike Morse
Greenhorn

Joined: Oct 31, 2000
Posts: 3
Thanks Thomas Paul,
I did some testing and was able to get it to work.
Here is a sample piece of code to show how it works.
Assume that the scrollpanel and Jtextarea are already created and the JTextarea is already added to the scrollpane.
//================================
int x;
jtextareaobj.selectAll();
x = jtextareaobj.getSelectionEnd();
jtextareaobj.select(x,x);
//=================================
This piece of code make the scollbar move to the end of the textarea object. It works but the selectAll could slow things down if there is a lot of text. If you can think of a better way let me know.
Thank You All,
Mike
Mike Morse
Greenhorn

Joined: Oct 31, 2000
Posts: 3
I just discovered that calling
jtextareaobj.selectAll();
by itself will work to.
Since the Jtextarea is not enabled this is not an issue for me.
But I am still concerned that SelectAll could cause problems
in very large messages.
Thanks
Mike
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Try this:
Document d = JTextArea1.getDocument();
JTextArea1.select(d.getLength(), d.getLength());
Andy PRO
Greenhorn

Joined: Mar 09, 2003
Posts: 1
Hey guys, check this out.
I think I found the most elegant way to do the autoscroll thing with the JTextArea.
This worked just fine for me...
I extended a myTextArea class from the original JTextArea class. It Looks like this:
----------------------------------
import javax.swing.JTextArea;
public class myTextArea extends JTextArea {
public myTextArea(int rows, int cols) {
super(rows,cols);
}
public void append(String text) {
super.append(text);
this.setCaretPosition(this.getCaretPosition()+text.length());
}
}
----------------------------------
So... everytime I make an append call to myTextArea the carret moves to the end of the TextArea and implicitly the scrolling component moves to the end.


Andy_:PRO
Mirklander Ismynick
Greenhorn

Joined: Oct 06, 2003
Posts: 1
Well, I think the best posible to do this is to add an adjustement Listener to the scroll bar, this way is how I do it, and works well, what you must change for your JText field is the value sumed to the Height, but I think if you put a largest value than the needed is not a problem, as example I can put same 100 or 10000 and works well.
Code for this is

JTextArea display= new JTextArea();
JScrollPane scroll =new JScrollPane(display);
scroll.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){
public void adjustmentValueChanged(AdjustmentEvent e){
display.select(display.getHeight()+1000,0);
}});
that does the autoscroll stuff for you.
See Ya
[ October 06, 2003: Message edited by: Mirklander Ismynick ]
VIJAY Yadlapati
Ranch Hand

Joined: Aug 04, 2003
Posts: 175
Another way is to use Robot.
jtextareaobj.requestFocus();
robot.keyPress(java.awt.event.KeyEvent.VK_CONTROL);
robot.keyPress(java.awt.event.KeyEvent.VK_END);
robot.keyRelease(java.awt.event.KeyEvent.VK_END);
robot.keyRelease(java.awt.event.KeyEvent.VK_CONTROL);
Fredt Ghost
Greenhorn

Joined: Apr 09, 2006
Posts: 1
There are many ways. I use this.


[ April 09, 2006: Message edited by: Fredt Ghost ]
[ April 09, 2006: Message edited by: Fredt Ghost ]
Orvar Ehn
Greenhorn

Joined: Oct 22, 2009
Posts: 1
And another one...


Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10486
    
    9

Orvar,

Welcome to the Ranch.
Please read http://faq.javaranch.com/java/DontWakeTheZombies
As you can see the last post is 3 years old. I doubt if the original poster is waiting that long for the solution


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JScrollpane - Force autoscroll to bottom