Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4270710
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:20:36+00:00 2026-05-21T07:20:36+00:00

I have a JTable with a JScrollPane. There is something i do not understand

  • 0

I have a JTable with a JScrollPane. There is something i do not understand with the scrollPane viewport … I am now selecting row number 1000 in the table, so many rows above it are not visible on the screen. Now when i check if row 0 is visible in the current viewport, it says ‘Yes’.
Here is my code :

    JViewport viewport = scrollPane1.getViewport();
    Rectangle rect = table1.getCellRect( 0, 1, true ); 

    // Check if view completely contains the row 0 :
    if( viewport.contains( rect.getLocation() ) )
        System.out.println( "The current view contains row 0" );

This code always returns true, and the text is printed, whatever the row i am standing on. Am i missing something here ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:20:36+00:00Added an answer on May 21, 2026 at 7:20 am

    The method you’r looking for is

     getVisibleRect()
    

    it’s defined in JComponent, in your context you use it

     table1.getVisibleRect().contains(rect)
    

    edit: just realized that you probably are still scratching your head – even though all answers already given are technically correct 🙂

    Basically, it’s all about coordinate systems, that is a location relative to a given origin. When using location related methods, you have to be aware of the coordinate system for that particular method is and you can’t mix different systems (at least not without translating the one or other).

    But mixing you did:

          // cellRect is in table coordinates
          Rectangle cellRect = table.getCellRect(...)
          // WRONG!!! use table coordinates in parent sytem
          table.getParent().contains(cellRect.getLocation());
    

    The solution is to find a coordinate system where the cell location as found above makes sense (or translate the cell location into the parent system manually, but that’s not needed here), there are methods which do the translation:

          // returns the visible part of any component in its own coordinates
          // available for all components
          Rectangle visible = table.getVisibleRect();
          // special service method in JViewport, returning the visible portion
          // of its single child in the coordinates of the child
          Rectangle viewRect = ((Viewport) (table.getParent()).getViewRect();
          // both are the same 
          visible.equals(viewRect)
    

    querying the table itself (as opposed to querying its parent) is preferable, because it doesn’t need any knowlegde about its parent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a jTable ( inside a jScrollPane ) with 1000+ columns. Is
I have a JTable that is within a JScrollPane. Rows are added to the
I have a JTable inside of a JScrollPane . I am creating a custom
I have this structure: <JFrame> <JPanel backgroundcolor = pink> <JScrollPane> <JTable>!!!Data here !!!</JTable> </JScrollPane>
I have a JTable which is held in a scrollpane, which in turn sits
I have a jTable as from the attached picture Right click on a row
I have a JTable that loads data from a database. Because sometimes there's too
So I have a bunch of JTable s. Each JTable is inside a JScrollPane
I have a JTable and i want a cell (or its row) painted in
I have a JScrollPane with a JTable in it. In the JTable I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.