Solved

scrollpane and scrollbar

Posted on 2001-06-14
6
253 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 100 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now