Solved

Change jbutton icon

Posted on 2006-07-05
3
420 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:List244
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 17048418
are you sure you are passing a valid icon/image
0
 
LVL 8

Author Comment

by:List244
ID: 17048440
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);
}
0
 
LVL 8

Author Comment

by:List244
ID: 17048507
Nevermind, you are right, wrong filename.  I forgot to retrieve the value of the text.. I was just using the number, thanks.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now