This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How to add images to a button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to add images to a button" Watch "How to add images to a button" New topic
Author

How to add images to a button

chanakya nani
Greenhorn

Joined: Jan 11, 2011
Posts: 29
Guys please help me in adding an image to the button..

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4398
    
    8

So what's happening at the moment? Don't ask us to guess what the problem is. TellTheDetails.
chanakya nani
Greenhorn

Joined: Jan 11, 2011
Posts: 29
Matthew Brown wrote:So what's happening at the moment? Don't ask us to guess what the problem is. TellTheDetails.


sorry but the Image is not getting added to the button
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

It doesn't make sense to say "the" button because you have 64 buttons, not just one.

But let's suppose you aren't seeing an icon on any of those 64 buttons. (Your description isn't very clear so it could mean other things, but let's start with that as a first guess.) Then that would mean your createImage() method is returning something which can't be seen by you.

There are other possibilities, like for example perhaps you aren't calling pack() or setVisible(true) on the JFrame at the right time... in fact I see you call setVisible(true) before you add the buttons; that might be the problem too. It's better to use a standard set of code (see the Swing tutorial for an example of a simple Swing program) rather than throwing together code at random.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Because you call frame.setVisible(true) before you add components you must call revalidate() and repaint() on the JPanel after you add or remove components.

And I'll move this thread to our GUI forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
chanakya nani
Greenhorn

Joined: Jan 11, 2011
Posts: 29
Paul Clapham wrote:It doesn't make sense to say "the" button because you have 64 buttons, not just one.

But let's suppose you aren't seeing an icon on any of those 64 buttons. (Your description isn't very clear so it could mean other things, but let's start with that as a first guess.) Then that would mean your createImage() method is returning something which can't be seen by you.

There are other possibilities, like for example perhaps you aren't calling pack() or setVisible(true) on the JFrame at the right time... in fact I see you call setVisible(true) before you add the buttons; that might be the problem too. It's better to use a standard set of code (see the Swing tutorial for an example of a simple Swing program) rather than throwing together code at random.





This is my code... i just created a chess board and i wanted to add the image of queen to the button...8 Queens problem....i did not write the loop yet..do you have any better ideas than looping the queens..??? please tell me
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4398
    
    8

I may be missing something obvious...but in the code you've just posted, createImage returns null. Which would explain why you can't see any image - there isn't one to see. Is that the correct code?
chanakya nani
Greenhorn

Joined: Jan 11, 2011
Posts: 29
Matthew Brown wrote:I may be missing something obvious...but in the code you've just posted, createImage returns null. Which would explain why you can't see any image - there isn't one to see. Is that the correct code?



yeah it is working fine...i can see the output, a chess board...and the code null is auto generated by eclipse when i tried to fix the bug
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4398
    
    8

So how do you expect an icon to be visible on the button, when you are explicitly setting that icon to null? What do you think the createImage() method is doing?
chanakya nani
Greenhorn

Joined: Jan 11, 2011
Posts: 29
Matthew Brown wrote:So how do you expect an icon to be visible on the button, when you are explicitly setting that icon to null? What do you think the createImage() method is doing?



can you please tell me what should i do..??
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4398
    
    8

Well, you want to create an ImageIcon. How do you usually create objects?

Look at the documentation of the ImageIcon class - you'll see it's got various constructors. At least one of them should take arguments that are suitable.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
can you please tell me what should i do..??


Read the tutorial on How to Use Icons for working examples.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Rob Spoor wrote:Because you call frame.setVisible(true) before you add components you must call revalidate() and repaint() on the JPanel after you add or remove components.

Perhaps now you'll be able to read my text.

After adding the following two lines add the end of the go() method showed a chess board for me:
However, you actually should revalidate the frame instead of the panel. Since JFrame does not extend JComponent it does not have the revalidate() method, but you can use invalidate() and validate() instead:
I'm making a bit of a fuss about this because it's also part of the problem; after copy-pasting your code I got a blank screen.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Rob Spoor wrote:
Rob Spoor wrote:Because you call frame.setVisible(true) before you add components you must call revalidate() and repaint() on the JPanel after you add or remove components.


True... but there's no obvious reason to call setVisible() at that point of the code. Probably it was put where it is in the first draft, then more code was carelessly added after it rather than before it. The most sensible fix to the can't-see-anything problem would be to just not call setVisible until the JFrame has been completely configured.
 
GeeCON Prague 2014
 
subject: How to add images to a button