Solved

programmatically setting a scroll bar

Posted on 2002-06-06
3
255 Views
Last Modified: 2010-08-05
I have a JScrollpane managing a (dynamic) JTable, and would like to scroll to the appropriate row when some action is taken.  As stated above, my table is dynamic, and for the purpose of this example, let's say that it will display either 10 rows or 20 rows of data.

The problem is that my call to "getMaximum()" does not seem to give me the correct results:

    JScrollBar theScrollBar =
      scrollPane.getVerticalScrollBar();
    double rowPosition =
          ( (i * 1.0) / sortModel.getRowCount() ) *
          ( (double)theScrollBar.getMaximum() );
    theScrollBar.setValue((int)rowPosition);

I placed some debug in my code, and sortMode.getRowCount() DOES give me either 10 or 20, as expected.  However, getMaximum seems to not know that the table is now 10 versus 20, or vice versa.
0
Comment
Question by:happyloman
[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
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 7060408
To scroll to a particular row try the following:

table.scrollRectToVisible(table.getCellRect(row, 0, true));
0
 

Author Comment

by:happyloman
ID: 7062799
Objects,

I tried your code, and it has the same behavior.

WHen the table goes from 10 to 20, the code is OK
When the table goes from 20 to 10, the code is NOT OK
(the table gets scrolled to the middle, but the cell I want is a the way way bottom.  It is not in
the viewport)
0
 
LVL 92

Expert Comment

by:objects
ID: 7063564
Sounds like there may be a problem with how you are manipulating the data in your table.
Can you post your code.
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

691 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