How can I put an image in my form?

Posted by Santeron on Stack Overflow See other posts from Stack Overflow or by Santeron
Published on 2010-03-17T13:49:54Z Indexed on 2010/03/17 13:51 UTC
Read the original article Hit count: 157

Filed under:
|
|

Hey guys,

I'm having a problem. I want to put an image inside a form in Java and I don't know if I'm using a proper technique (found it somewhere in a web page).

private void iconSelect() {
    String iconString = "";
    if (typeCombobox.getSelectedIndex() == 0) {
        iconString = "LP_";
    } else if (typeCombobox.getSelectedIndex() == 1) {
        iconString = "HP_";
    } else if (typeCombobox.getSelectedIndex() == 2) {
        iconString = "BP_";
    } else if (typeCombobox.getSelectedIndex() == 3) {
        iconString = "BS_";
    }
    if (RB_Gain_Clean.isSelected()) {
        iconString = iconString + "Clean";
    } else if (RB_Gain_dB.isSelected()) {
        iconString = iconString + "dB";
    }

    ImageIcon icon = new ImageIcon("images/" + iconString + ".jpg");
    Image img = icon.getImage();
    if (iconGraphLabel.getWidth() > 0 && iconGraphLabel.getHeight() > 0) {
        img = img.getScaledInstance(iconGraphLabel.getWidth(), iconGraphLabel.getHeight(), java.awt.Image.SCALE_SMOOTH);
    }
    icon = new ImageIcon(img);
    iconGraphLabel.setIcon(icon);
}

So it actually shows the image and it is resizing but when I resize my form and then make it smaller again, the label doesn't seem to follow the resizing so it stays bigger than the window.

Also, since I'm not very familiar with java's graphics, can anyone tell me how can I control a window resizing event so I redraw the picture? Right now the method is triggered by the combobox and the radiobuttons shown in the code.

Thanks in advance!

© Stack Overflow or respective owner

Related posts about java

Related posts about image