tags:

views:

21

answers:

1

I have a panel with several JLabel, I would like to change all their Icon,

String path = System.getProperty("user.dir");

for (int x=0;x< 21;x++) {
    javax.swing.JLabel lab = boardPanel.getComponent(x).;
    lab.setIcon(new ImageIcon(path + "\\image\\blank.jpg"));
} 

it gives me an error of incompatible type, all inside the boardPanel are JLabel, im using netbeans 6.8.

+4  A: 

getComponent() will return a Component. You'll need to cast to a JLabel.

javax.swing.JLabel lab = (javax.swing.JLabel)boardPanel.getComponent(x);

For safety's sake you should check the expected type before casting. After all, at some stage you may have types in there other than JLabels.

Component c = boardPanel.getComponent(x);
if (c instanceof JLabel) {
   JLabel lab = (JLabel)c;
   // etc.
}
Brian Agnew
wow that was fast, thanks for the advice, it works!
Josh
No problem. That's what we're here for :-)
Brian Agnew
@Josh How about accepting the answer if it works, otherwise it will be listed unanswered.
stacker