Ok, so I have a JButton on my program
and later on in the program i want to put an image on the JButton

I have many buttons that start out with images on them, but Im wondering how to add images to the JButton at a later date

so I have the Image here

I1 = new ImageIcon ("Images/image.jpg");

Then later on i want to add the image, I tried all types of things, but nothing works
thanks in advance

Look at the API doc for the JButton class and the class it extends. There are methods that will help you. If you have any questions about how to use a method, copy its doc here with your question.

ok, will do

but one thing, I'm thinking that JButton.setIcon(icon);
should work, thats what i've been trying to work with but to no avail
I even tried repaint, validate and all that stuff

Can you post a small program that compiles and executes that shows the problem?

here is a small example, but just found out that some images work and some dont, and i want you to use the image that I'm using

here the link

http://www.google.ca/imgres?imgurl=http://appsamuck.com/assets/images/day17_icon_32.png&imgrefurl=http://appsamuck.com/&usg=__1PUBc1Hw7yvSDLWdiZz8OdEXTA8=&h=32&w=32&sz=3&hl=en&start=51&zoom=1&tbnid=iy8uxYAJZBBA8M:&tbnh=32&tbnw=32&ei=E7IkT-7-K6no0QGb7fTKCA&prev=/search%3Fq%3Dfireball%26start%3D40%26hl%3Den%26safe%3Dvss%26sa%3DN%26biw%3D1280%26bih%3D693%26gbv%3D2%26sout%3D1%26tbs%3Disz:i%26tbm%3Disch&itbs=1

the fireball icon

heres the code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

//just an example

public class ProblemExample extends JFrame implements ActionListener
{
    static Container contentPane;
    static JPanel infoPane;
    static JButton pButtonarray[] [] = new JButton [15] [15];

    public ProblemExample ()
    {
        contentPane = getContentPane ();
        if (infoPane != null)
        {
            contentPane.remove (infoPane);
        }
        infoPane = new JPanel ();
        infoPane.setLayout (null); //setting layout to null so i can use absolute positioning with coordinates



        //Button grid 
        
        int ppositionx = 50, ppositiony = 175;
        
        for (int row = 0 ; row < 15 ; row++)
        {
            ppositiony = ppositiony + 23;
            ppositionx = 50;

            for (int x = 0 ; x < 15 ; x++)
            {
                pButtonarray [x] [row] = new JButton ();
                pButtonarray [x] [row].setOpaque (false);
                pButtonarray [x] [row].setBounds (ppositionx, ppositiony, 25, 25);
                pButtonarray [x] [row].addActionListener (this);
                pButtonarray [x] [row].putClientProperty ("px", new Integer (x));
                pButtonarray [x] [row].putClientProperty ("prow", new Integer (row));
                infoPane.add (pButtonarray [x] [row]);
                contentPane.add (infoPane);
                ppositionx = ppositionx + 23;
            }
        }
        contentPane.add (infoPane);
        validate ();
    }





    public void actionPerformed (ActionEvent e)
    {
        JButton btn = (JButton) e.getSource ();


        //image
        ImageIcon image;
        image = new ImageIcon ("fire.gif");

        //getting coordinates
        int px = ((Integer) btn.getClientProperty ("px")).intValue ();
        int prow = ((Integer) btn.getClientProperty ("prow")).intValue ();

        pButtonarray [px] [prow].setIcon (image);

        System.out.println ("Image set");
    }

    public static void main (String[] args) throws IOException //main
    {
        ProblemExample window = new ProblemExample ();
        window.setTitle ("Problem Example");
        window.setSize (1000, 750);
        window.setVisible (true);
    }
}

Can you explain what the problem is? How can I see it?
All squares I click on have an image displayed after the click.


See you tomorrow.

Can you explain what the problem is? How can I see it?
All squares I click on have an image displayed after the click.


See you tomorrow.

Did you use the image that i provided, save the gif image as fire, as this is the image i need. Other images will be displayed on the JButton, but this particular image isn't, even though the program says it is by printing out "image set"

and sorry for the late reply,

Does the image work/display in other programs?
Here's what I see:

new ImageIcon(...) is famous for not throwing any exceptions when it fails to find the file; it just returns null. JLabel is happy to accept null for its image icon, so the final result is no image and no error messages. We see that quite often in DaniWeb "help!" posts.
After a new ImageIcon(...) it's always a good idea to test for null and diagnose or throw an exception so you know what went wrong.

new ImageIcon(...) is famous for not throwing any exceptions when it fails to find the file; it just returns null. JLabel is happy to accept null for its image icon, so the final result is no image and no error messages. We see that quite often in DaniWeb "help!" posts.
After a new ImageIcon(...) it's always a good idea to test for null and diagnose or throw an exception so you know what went wrong.

how doi check if it is null?

if (image == null) ...

if (image == null) ...

well, its not null so thats not the problem, I'm going to check if the image works in other program like norm said

That doesn't mean anything. Try this test:

image = new ImageIcon ("images/XXXfireball.png"); //fire.gif");

  ....
  System.out.println ("Image set image=" + image);

It prints:
Image set image=images/XXXfireball.png
Image set image=images/XXXfireball.png
Image set image=images/XXXfireball.png

The file does NOT exist, but there is an image object!!!

Use the exists() method of the File class with the path/name of the file to see if the program can find the file.

Well I'll be blowed! It seems I've had that wrong all along. Thanks for setting me right Norm.

That doesn't mean anything. Try this test:

image = new ImageIcon ("images/XXXfireball.png"); //fire.gif");

  ....
  System.out.println ("Image set image=" + image);

It prints:
Image set image=images/XXXfireball.png
Image set image=images/XXXfireball.png
Image set image=images/XXXfireball.png

The file does NOT exist, but there is an image object!!!

Use the exists() method of the File class with the path/name of the file to see if the program can find the file.

thanks alot, I dident define the path right, how stupid can i get, i even looked at it like 10 times

You can use
btn.setIcon(new javax.swing.ImageIcon("star.png"));
ImageIcon class can help you to set Image As icon for button in Java, but it can show image with original height and width. you can not resize image here when you use it an icon on jbutton.

Hello Rohini, welcome to DaniWeb

Your contributions are welcome, but please take time to read the whole of a thread before posting.
Firstly, this thread is 3 years old, so any reply will be too late.
Secondly, the code you post is effectively what the original question had, and which didn't work. That was the whole point of this thread. Read the whole thread to see why.
Thirdly, the link you provided was to some trivial code that totally fails to address the problem in this thread.

JC

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.