Solved

programmatically setting a scroll bar

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars2 challenge 6 67
json example 39 111
wordcount challenge 11 74
@SBGen Method 3 25
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

708 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

13 Experts available now in Live!

Get 1:1 Help Now