Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Having trouble getting jlabel icons to change size with window resize?

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm trying to create a chess program in java swing, and for the board I'm using an 8 by 8 array of jlabels stuck inside a jpanel with a grid layout. I would like the board to change size when the window changes size, and the grid layout accomplishes this succesfully, until I add the actual icons to the jlabels. Then the board just stays a fixed size. Someone suggested I use their "Stretch Icon" class, found here: http://www.camick.com/java/source/StretchIcon.java, so I tried initializing the icons using one of the constructors in the class (the one that takes an image name and a boolean value for whether or not you want the resisizing to be proportionate (to maintain the aspect ratio)). However, the same thing happened. The board appears, but it does not resisize. To attempt to fix this problem, I tried simplifying my code, reducing the program to two jlabels within the jpanel, for which I then tried to set the image in the same way. Here is my simplified code:



Anyone know if I'm misusing the stretch icon class, or just generally what's going wrong/how to fix it?
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A FlowLayout always displays a component at its preferred size.

If you want the grid to change size, then use a GridLayout. Read the section from the Swing tutorial on How to Use Grid Layout for more information.

Also fix your code tags. The end tag should be "code" not "java".
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, I changed it and it worked! What I have now is:



Thanks a lot!
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A new question now though, is there any way I can lock the aspect ratio of the gridlayout? Right now the individual chessboard panels keep their aspect ratios, but since the panel containing them does not, they space out differently depending on what shape the panel is. And since this is a chessboard, I would like there to be no space between any of the panels. Do you know if there is a way to do this?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!