Solved

Change jbutton icon

Posted on 2006-07-05
3
428 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

733 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