?
Solved

Change jbutton icon

Posted on 2006-07-05
3
Medium Priority
?
431 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 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