0

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.

Default size with appropriate image sizes

Resized larger, images same size

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.

Michael Yousef
  • 602
  • 3
  • 11
  • 22
  • 5
    Resizing the labels is the job of the layout manager of the container where the labels are. So the problem source is that layout (or the container which holds that, and so on). – kiheru Aug 17 '13 at 21:20
  • 2
    [Don't use `setPreferredSize()` when you really mean to override `getPreferredSize()`](http://stackoverflow.com/q/7229226/230513). – trashgod Aug 17 '13 at 21:25
  • I'm using a layout manager that should have this feature. I also tried overriding my ImageLabel's getPreferredSize() to some horrible results. I've added some extra information to the original post to try so you can have a better understanding of the issue. – Michael Yousef Aug 18 '13 at 05:05
  • 1
    You'll need two things: 1. setup the layout manager to give all the available space to the image labels (I'm not familiar with MigLayout, so I don't know how that's done with it). 2. Make the labels rescale their icons when the labels are resized; there you can use a resize listener. (Just keep one original image to always use as the source for the scaling, otherwise the image quality will degrade with multiple resizes). – kiheru Aug 18 '13 at 08:09

0 Answers0