• Post Reply Bookmark Topic Watch Topic
  • New Topic

Resizing swing components  RSS feed

 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IF you resize a window containing swing components how do you resize the
contents to stay in proportion?

Thanks.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use a Layout Manager then that is all handled for you. If you are using a null layout and specifying each components pos and size with setBounds(...) and you want to resize each component to fit then I feel for you, really I do.
[ February 08, 2005: Message edited by: Gregg Bolinger ]
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using a null layout and specifying each components pos and size with setBounds(...) and you want to resize each component to fit then I feel for you, really I do.
Completely agree! However... there are those rare instances when you still need to do a little tweaking to get the components to resize correctly. For instance, if you have a JScrollPane in a container using SpringLayout, the preferred size of the JScrollPane is based on the viewport size of the component it holds. When the JFrame resizes, it doesn't necessarily build the Springs correctly...

Anyway, you can add a ComponentListener to the JFrame to catch when it is resized. This code adjusts the preferred size of a JScrollPane based on the JFrame's size:
Note the revalidate() call on the JScollPane and the validate() call on the JFrame. This is an old trick I've found to make sure the components repaint with the correct sizes... Keep in mind that I only use this code if I absolutley have to, like with the JScrollPane example mentioned earlier. And I always use a LayoutManager!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!