Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem of using BorderLayout

 
Yingie Pitts
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use BorderLayout one a InternalFrame to arrange three components: JPanel (North), JTree (West) and JTable (East). My problem is the size and location of the JTable.
I used setPreferredSize() and setLocation() but none of them seems work properly. The size is not what I set and the location changes when I drop the InternalFrame. Have you had any similar experience and want to share?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BorderLayout ignores many of the preferred/max/min dimensions of the components it manages. As the Javadoc says,

The components are laid out according to their preferred sizes and the constraints of the container's size. The NORTH and SOUTH components may be stretched horizontally; the EAST and WEST components may be stretched vertically; the CENTER component may stretch both horizontally and vertically to fill any space left over.

If you need more control, consider using GridBagLayout instead.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, since JTable really works the best in a JScrollPane, some of these methods should be applied to the JScrollPane rather than the JTable. JTable also supplies a method with a really long name that helps a lot when dealing with preferred size issues - setPreferredScrollableViewportSize(). Basically, this method tells the scroll pane what size it needs to be when it contains this table. I usually calculate the Dimension passed into it off of the table's column and row sizes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic