Solved

scrollpane and scrollbar

Posted on 2001-06-14
6
257 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
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!

 

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

Industry Leaders: 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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

734 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