Link to home
Start Free TrialLog in
Avatar of happyloman
happyloman

asked on

programmatically setting a scroll bar

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.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of happyloman
happyloman

ASKER

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)
Sounds like there may be a problem with how you are manipulating the data in your table.
Can you post your code.