Solved

Change jbutton icon

Posted on 2006-07-05
3
429 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

726 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