?
Solved

scrollpane and scrollbar

Posted on 2001-06-14
6
Medium Priority
?
260 Views
Last Modified: 2010-03-31
i wonder how to control a scrollbar together with a scrollpane? can any one provide me a simple example on this.

what if i want the scrollbar to always scroll down to the last page/end of file, what should i do? can any provide me the example/source on this?
0
Comment
Question by:cys050200
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:saurav17
ID: 6193962


hi,
pls., be clear on what u r saying.
I dont get ur concept., Could u give me more details abt., what u want.


regards,
K.J.S.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6193963
simply set all the time the caret from your text object used for displaying to point to the end of the document, call an scroll.updateUI(); and that's all.
0
 
LVL 1

Expert Comment

by:saurav17
ID: 6193976


hi,
pls., be clear on what u r saying.
I dont get ur concept., Could u give me more details abt., what u want.


regards,
K.J.S.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:cys050200
ID: 6194209
for example, here are some codes for a class which extends JFrame:

 JTextArea outputArea = new JTextArea(30,60);
 JScrollPane scroller = new JScrollPane(outputArea);
 String str = "xxxx" ; //the str may be very long

 scroller.setAutoscrolls(true);
 outputArea.setAutoscrolls(true);
 outputArea.setText(str);
 
 Container c = getContentPane();
 c.setLayout(new BorderLayout());
 c.add(scroller,BorderLayout.CENTER);

 my question is, in the above situation, why is that my output(the scroller) is not scrolled down automatically if the str input is very long(longer than a page, for example).
0
 
LVL 9

Accepted Solution

by:
Ovi earned 300 total points
ID: 6194409
Do'it like this :

JTextArea outputArea = new JTextArea(30,60);
JScrollPane scroller = new JScrollPane(outputArea);
String str = "xxxx" ; //the str may be very long

scroller.setAutoscrolls(true);
outputArea.setAutoscrolls(true);
outputArea.setText(str);
updateScroll();

Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add(scroller,BorderLayout.CENTER);
.....

private void updateScroll() {
outputArea.setCaretPosition(outputArea.getText().length() - 1);
}

Every time you update the text or append some text to your JTextArea call updateScroll() method.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6194418
you may need an try/catch in the method updateScroll for invalid location like this :

private void updateScroll() {
  try {
    outputArea.setCaretPosition(outputArea.getText().length() - 1);
  catch(Exception e) {
    System.out.println(e.toString());
  }
}

What is done at setCaretLocation ? The caret position is updated after that is build an Rectangle object based on the new location and called the scrollPane method scrollRectToVisible(Rectangle r)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question