Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

JScrollPane JViewport question

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTable has a method called setPreferredScrollableViewportSize( Dimension ).

In addition to being one of the longest method names I have ever seen in Java, it correctly sets the preferred size of any scroll pane that holds this JTable.
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!