Solved

programmatically setting a scroll bar

Posted on 2002-06-06
3
250 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
json example 39 132
Core Java. What output will be and why ? 6 66
Apps blocked by Java 9 63
expectj telnet failing 5 26
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…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now