I am adding a JTable to a JScrollPane...which sits on a JPanel My problem is how can I reduce the size of the JViewport so that it is exactly the size of the JTable...right now...when I fit the JTable in the JScrollPane, there are huge gaps both width and height wise. I have tried using several methods setting maximum size for JScrollPane etc, and even setting size for JViewport...but as soon as I add a JTable to it, everything falls back to the original problem. Anyone has faced this problem before? Have links to code? Thanks.
If the table has a smaller size, than the scrollpane, then the viewport takes the same size like the scrollpane. I think, this is your case. The only thing you could do is making the table a bit larger. You can do this with following call:
Then your table has the same width, like your scrollpane. But the height will stay the same. There you can do some stuff with your LayoutManager, like using GridBagLayout and setting the weight to 0 and so on. But I wouldn't do that, because if your table becomes larger, you will run in problems. Hope this helps Rene
hi, I have a similar problem, so I am not going to start a new thread. I call pack() on my JFrame containing to JScrollPanes and some other components. I have set the preferred sizes of both scrollpanes' viewports and I have set the minimum size of both, as well. When the application starts, pack will compute the size of the JFrame to show the scrollpanes with their preferred sizes. But when I resize the frame in height (width is not that delicate), the scrollpanes will be shrinked to their minimum sizes. They aren't scaled to fill in the space that is left in the JFrame. It's either preferred size or minimum size. The one scrollPane that is the more important is inside a JPanel with a CardLayout. I add this panel to the main container setting its weighty to 1 (GridBagLayout). but that doesn't help. any ideas? thanks Chantal
posted 17 years ago
I solved it myself. for the ones having similar problems: I just changed the constraint GridBagConstraints.fill of the panel containing the scrollpane from: HORIZONTAL to BOTH.
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.