tags:

views:

46

answers:

2

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!

edit1: Well the form is my jFrame. The iconGraphLabel is the jLabel I'm putting the image in. I'll try to explain the hierarchy of the parent components.

PlotArea [jPanel] (cardLayout) > plotArea_Image [jPanel] ("cardDraw") > iconGraphPanel [jPanel] > iconGraphLabel

A: 

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

Correct, a JLabel, or any Swing component that uses Icons will paint the Icon at its actual size. If you want the Icon to scale depending on the space available then you need to do custom painting.

The Background Panel classes provides different options for displaying an image (you can just use the Icon,getImage() method). You should also read the section from the Swing tutorial on Custom Painting to better understand how the above code works.

camickr
Thanks for the help mate, seems nice but I can't make it work. Is there any way i can put code in comment so I can show you?? :P
Santeron
A: 

Found a solution. This is the final code:

private void iconSelect() {
    iconGraphPanel.removeAll();
    ImageIcon icon = new ImageIcon("image.jpg");
    BackgroundPanel imagePanel = new BackgroundPanel(icon.getImage(), BackgroundPanel.SCALED);
    iconGraphPanel.add(imagePanel);
    iconGraphPanel.revalidate();
}

iconGraphPanel is a common jPanel that I use as a place holder. It needs to be set to BorderLayout. The BackgroundPanel class can be found here. The removeAll() is needed so the old image disappears. If you don't put this images start to stack up. Don't know if there is a better way to do that but it works just fine for me. The revalidate() method is needed because we create a new panel so it needs to refresh.

This is mostly camickr work and some other guy from sun forum named Maxideon. I'm just posting for future reference.

Santeron