Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JScrollpane - Force autoscroll to bottom

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Try this:
Document d = JTextArea1.getDocument();
JTextArea1.select(d.getLength(), d.getLength());
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 1
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
And another one...


 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic