Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I scroll up to the top of a JTextPane?

Posted on 1999-07-21
4
Medium Priority
?
491 Views
Last Modified: 2012-06-21
When I add a large number of strings to a small JTextPane, the view always scrolls to the last string.

How can I stop it scrolling, or return the view to the top?

Here's what I'm doing (v is a Vector containing about a hundred String elements)...

Document doc = textPane.getDocument();
for (int i = 0; i < v.size(); i++)
   doc.insertString(doc.getLength(), (String)v.elementAt(i), attrs);

Thanks.
0
Comment
Question by:wagtail
[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
4 Comments
 
LVL 1

Accepted Solution

by:
masato earned 300 total points
ID: 1246714
I'm not familiar with Swing, but could you try the setCaretPosition method as follows:

Document doc = textPane.getDocument();
int size = v.size();
for (int i = 0; i < size; i++)
    doc.insertString(doc.getLength(), (String)v.elementAt(i), attrs);
doc.setCaretPosition(0);

One suggestion is that v.size() should be outside the "for" loop to get the better performance.

Please reject if it is not working.
0
 
LVL 1

Expert Comment

by:masato
ID: 1246715
Oops, doc.setCaretPosition(0); should be textPane.setCaretPosition(0);
0
 
LVL 1

Expert Comment

by:masato
ID: 1246716
Oops, doc.setCaretPosition(0); should be textPane.setCaretPosition(0);
0
 

Author Comment

by:wagtail
ID: 1246717
Thanks masato, that works!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 …
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 …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

715 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