Solved

scrollpane and scrollbar

Posted on 2001-06-14
6
256 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to add new optional parameter to JSP 1 49
servlet example 17 53
How do I remove an object from a 3 40
jmss example java 2 24
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

820 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