• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

How to place image on JButton?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My java code below is trying to display a image on a JButton. Right now I am getting a compile error at Component b5= or at f.add(b5,BorderLayout.CENTER); . I am trying to auto correct with IDE Eclipse. All I want to do is be able to compile the code so I can display the image.

 
Marshal
Posts: 25483
67
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ching Ming wrote:All I want to do is be able to compile the code so I can display the image.



Compiling the code is the second step. The first step is figuring out what code to write -- and you are on the wrong track with that code.
 
Ching Ming
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how to find What data type does ImageIO.read() return?
 
Carey Brown
Saloon Keeper
Posts: 7114
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See: ImageIO Javadocs.

You will see that it doesn't return a Component but a Buffered Image which you have to draw() onto a Component like a JPanel.
 
Rancher
Posts: 3163
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

a Buffered Image which you have to draw() onto a Component like a JPanel.


You can create an ImageIcon using the BufferedImage and then add the Icon to the JButton.
 
Rancher
Posts: 4553
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eclipse will also tell you.
Hover your pointer over the method and the Javadoc will appear in a pop over box.

Since you want this image on a JButton then you ought to make b5 a JButton and not a base Component.

So the next step is to look at the JButton Javadoc and see if you can spot a suitable constructor for creating a button that has an image.

And there is one, which takes an Icon.

A quick click through to the Icon Javadoc and we see (under the list of All Known Implementing Classes) an ImageIcon, which sounds like the sort of thing we might want.

And the Javadoc for that...leads us to a link to the How To Use Icons tutorial.
 
Would you like to try a free sample? Today we are featuring tiny ads:
ScroogeXHTML 8.7 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/730700/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic