I have a JPanel that has some JLabels on it. They've been set to specific sizes based on the size of the JPanel. However, when I resize the JPanel, they don't get resized. What I would like to do is force them to get resized. Here was my attempt:
public void componentResized(ComponentEvent e) {
//just test values
int height = 10;
int width = 10;
Dimension d = new Dimension(width, height);
//whenever we detect this event, we'll resize the draft components
for(int i = 0; i < jpPack.getComponentCount(); i++){
jpPack.getComponent(i).setPreferredSize(d);
}
jpPack.revalidate();
jpPack.repaint();
}
However, this didn't work, I didn't get any results. I also tried with setSize(d) but nothing either. The way I've created it right now, the size of the JLabel is set at creation based on the size of the JPanel.
Essentially, all I want to do is tell the JLabel "the GUI has resized, here is your new size, now repaint yourself to be this size". Would I perhaps have to change my JLabel's paint method to handle this? If so, how would I go about that?
Thanks.
EDIT: Here is some additional information to try to help. I'm sorry I can't provide a working example, but there's just so much code involved in making this work and it isn't feasible (it's not a small project, it's a minor bug in a large project).
ImageLabels are my custom JLabel to use for my cards. There isn't a lot in there besides some stored data and compare methods. The paintComponent method when drawing the cards in the images below should just be calling the super method.
My JPanel containing the labels is defined as follows:
jpCards = new JPanel(new MigLayout("insets 0, gapx 0, gapy 0, rtl", "grow"));
When I add images, they are added as follows:
for(int i = 0; i < imgJLabels.length; i++){
BufferedImage img;
try{
img = ImageIO.read(new URL(XML.getXML().getCardURLByName(pack[i], set)));
}
catch (java.net.MalformedURLException e){
}
int height = (jpPack.getHeight() / 2);
if(height > 310) height = 310;
float temp = (float)img.getWidth() / img.getHeight();
int width = Math.round(height * temp);
if(width > 223) width = 223;
ImageIcon imgIcon = getScaledImage(img, height, width);
imgJLabels[i] = new ImageLabel(img, imgIcon, pack[i]);
jpPack.add(imgJLabels[i]);
imgJLabels[i].addMouseListener(this);
}
And my scaling method is as follows:
private ImageIcon getScaledImage(Image srcImg, int height, int width){
BufferedImage resizedImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, width, height, null);
g2.dispose();
return new ImageIcon(resizedImg);
}
Here you can see the results. As I resize the GUI to be bigger, the ImageLabels don't increase in size. The same applies when I make the GUI smaller, the cards don't become smaller.
I do have a maximum size for the cards set, as you can see. However, the large card image on the right is set to the same maximum, so my cards are definitely not hitting the maximum. The GUI is just not resizing the elements.
This issue has been driving me nuts for a long time and I have no idea what to do. Thanks again.