This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
    Bookmark Topic Watch Topic
  • New Topic

JScrollpane - Force autoscroll to bottom

 
Mike Morse
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Mike Morse
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Try this:
Document d = JTextArea1.getDocument();
JTextArea1.select(d.getLength(), d.getLength());
 
Andy PRO
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Mirklander Ismynick
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 175
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 1
  • 2
  • Mark post as helpful
  • send pies
  • Report post to moderator
And another one...


 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
    Bookmark Topic Watch Topic
  • New Topic