Skip to main content

Programmatically Scroll a JTable Inside a JScrollPane

4 replies [Last post]
retierney
Offline
Joined: 2008-10-23

Hi All,

I searched the forums and couldn't find an answer to my question. I have a JTable instantiated inside of a JScrollPane. I have implemented a search on one of the columns. It works just fine. I am able to find all the correct rows and highlight them one by one. My problem is -- how to scroll so that the selected row is visible.

I found scrollRectToVisible on ViewPort, but I can't work all the way back to the selected row. Can anyone help? Thank you very much!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
pietblok
Offline
Joined: 2003-07-17

Hi,

Not tested, but I guess this should do the job:

[code]table.scrollRectToVisible(table.getCellRect(row, column, false));[/code]

It is the scrollRectToVisible of your target component that should be invoked, not JViewPort's.

Piet

nikhilsarnaik
Offline
Joined: 2014-02-19

This works perfectly for me, thanks

sbozian
Offline
Joined: 2010-08-30

Hi,

I have a related question. I have a JTable inside a JScrollPane (let's call it table's scrollpane) too, but somewhere up in the hierarchy it happens that I have another JScrollPane (let's call it outer scrollpane).

So based on particular layout scenarios, sometimes the tables's scrollpane has:
-its vertical scrollbar showing
-its horizontal scrollbar not showing

while the outer scrollpane has its horizontal scrollbar showing.

My problem is when I call scrollRectToVisible on my JTable to scroll horizontally, it does nothing because the horizontal scrollbar is not showing (in fact all the table is 'viewable' in its viewport), while in reality the Rect is not 'visible' or 'showing' on the screen because it's not in the visible section of the outer scrollpane.

So the desired behavior is that, the outer scrollpane does the scroll.
So how can I modify the code of scrollRectToVisible to achieve that?

Thanks a lot,
Shant

retierney
Offline
Joined: 2008-10-23

Hi Piet,

It works like a champ. I completely missed the fact that JTable inherited the scrollRectToVisible method. Thanks a ton...

Bob