Link to home
Start Free TrialLog in
Avatar of List244
List244

asked on

Change jbutton icon

I have a jbutton where I use setIcon to try and change it, however it does not change.  I have
tried Button.invalidate() Button.revalidate() and Button.repaint()... None of this has caused the
image to change. Any suggestions?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of List244
List244

ASKER

public void keyReleased(KeyEvent KE) //Text area handler
{
      for(int i=0;i<5;i++)//5 texts match with 5 Dice
      {
            if(KE.getSource() == Text[i])
            {
                  if (Integer.parseInt(Text[i].getText()) > 6)
                        Text[i].setText("" + 6);
                  else if (Integer.parseInt(Text[i].getText()) <1)
                        Text[i].setText("" + 1);
                  URL Image = getClass().getResource("/"+String.valueOf(i+1)+ ".jpg"); //Images are all named 0.jpg ,1.jpg,2.jpg,3.jpg (through 6)
                  Icon ThePic = new ImageIcon(Image);
                  Dice[i].setIcon(ThePic);
            }
      }
}
There is the code I am using to set the images based on the value of a textfield.  It is identical to the code
which sets them to start with including the for-loop:

for(int i=0;i<5;i++)
{
      Dice[i] = new JButton();
      URL Image = getClass().getResource("/"+String.valueOf(i+1)+ ".jpg");
      Icon ThePic = new ImageIcon(Image);
      Dice[i].setIcon(ThePic);
}
Avatar of List244

ASKER

Nevermind, you are right, wrong filename.  I forgot to retrieve the value of the text.. I was just using the number, thanks.