Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

scrollbar problem resizing jtable in gridbag  RSS feed

 
Nigel Patching
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've got a table with a jscrollpane in a gridbag layout. I've set the gridbag constraints for the table to:
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
so that when the frame is resized the table expands. This all works fine.
The problem arises when the frame is expanded to a width greater than the table. When this happens the vertical scrollbar detached itself from the side of the table and keeps attached to the right hand edge of the frame. This leaves a grey gap between the right hand edge of the table and the vertical scrollbar.
Does anyone have any ideas how I might keep the scollbar attached permanently to the right hand edge of the table, no matter how wide the frame becomes.
Thanks
 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nigel,
What's happening is that the table is expanding but the sum of the cell widths is not. This gives you a gap between the cells and the table edge. The default background for the area beneath the cells is gray.
One possible solution could be to set up a large maxWidth for the cells so the sum of them will never be exceeded by the table background.
Hope that helps,
Terry
 
Nigel Patching
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice Terry. That would certainly work if I had enough cells and they are wide enough (as you said).
However, I am curious to know whether there is another solution to this problem which allows me to keep the cell widths relatively narrow.
Nigel
 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Try seting the scrollable part of the table dynamically as content is added ( or fixed at the start if you prefer)
use:
table.setPreferredScrollableViewportSize( Dimension d )
Hope that does the trick,
Terry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!