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 5992177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:29:38+00:00 2026-05-22T23:29:38+00:00

I have a JScrollPane with a JTable in it. In the JTable I have

  • 0

I have a JScrollPane with a JTable in it. In the JTable I have initially 3 rows. Later on rows get added.

The default JTable with my 3 rows is ugly because JScrollPane calls getPreferredScrollableViewportSize from the client (JTable) and the return value is always the same. That’s why my JTable/JScrollpane with 3 rows has this free space, JScrollpane andJTable do not have the same size.

My solution is to make a subclass JTable, override getPreferredScrollableViewportSize and return in this function getPreferredSize();. Now the whole JScrollPane has exactly the size of the 3 row `JTable!

When a row gets added I have the command ((JComponent) scrollPane.getParent()).revalidate();

The scrollpane grows with the table.

Everything works fine!

But now I want to set a certain layout for the container of the scrollpane (panel):
myjpanel.setLayout (new BoxLayout(contentPane, BoxLayout.Y_AXIS));

When I add this command my whole solution doesn’t work anymore.
The scrollpane hasn’t the size of the 3 row table, this free space is there again.

Why is this?

Does anybody have a solution?

CONTINUED

Thank you camickr for pointing me in the right direction. My solution is the following:

    scrollPane = new JScrollPane (table) {          
      public Dimension getMaximumSize () {
        double height = 0;

        double width = super.getMaximumSize().getWidth();
        height += columnHeader.getViewSize().getHeight();           
        height += viewport.getView().getPreferredSize().getHeight();
        height += (getViewportBorderBounds().getHeight() * -1);
        Dimension returnValue = new Dimension ((int) width, (int) height);
        return returnValue;
      }
    };

It works now at the beginning.
But when a row is added to the jtable and I call revalidate(); on the jpanel (parent container of the scrollpanel) the jscrollpanel shrinks in height to 1 row + header!
Has anyone got an idea what is to do in this situation.

CONTINUED

Now I know the problem. It is the viewportBorder. In order to get the whole, exact height I have to total the height of the view (Jtable), the height of the column header and the height of the viewportBorder. The first time getViewportBorderBounds().getHeight() returns -3, next time – after a new row is inserted in the table model – the function returns 48. I do not understand why this function returns 48 now.
Another point is that camickr says that changing the size of the jscrollpane defeats the purpose of the scrollpane. I do not understand this. How can anyone be satisfied with a default jscrollpane size of 450 x 400 (http://www.javalobby.org/java/forums/t19559.html) even when the jtable has only 3 lines at the beginning. This unfilled extra space does not look very professional in my eyes. Has anyone got a better solution for this.

Thank you very much for your answers

CONTINUED
Now everything works!
At the beginning I simply set the jscrollpane border to 0 … sp.setBorder(createEmptyBorder());
In this way I avoid having these strange return values of getViewportBorderBounds().getHeight()
Wolfgang

  • 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-22T23:29:39+00:00Added an answer on May 22, 2026 at 11:29 pm

    The BoxLayout allows the component to grow up to the maximum size of the component. I guess you need to override the getMaximumSize() of the scrollpane to return the preferred size.

    When a row gets added I have the command “((JComponent) scrollPane.getParent()).revalidate();” The scrollpane grows with the jtable.

    That kind of defeats the purpose of the scrollpane. The scrollbar is supposed to stay at a fixed size and then scrollbars will appear if necessary.

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

Sidebar

Related Questions

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>
Hi I have created a Jtable and can get it to show on my
I have a JTable inside a JScrollPane. In one of the columns in the
Have you managed to get Aptana Studio debugging to work? I tried following this,
Have you refactored from an ActiveRecord to a DataMapper pattern? What conditions prompted the
I have a JTable with a custom TableModel called DataTableModel . I initialized the
I have 2 windows one window shown the JTable Model data, when double click
I'm having small problem (I guess) of showing the JTable panel. I have class

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.