Solved

scrollpane and scrollbar

Posted on 2001-06-14
6
258 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…

705 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