wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JButton background in SWING Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JButton background in SWING" Watch "JButton background in SWING" New topic
Author

JButton background in SWING

Adriano Bellavita
Ranch Hand

Joined: Mar 11, 2010
Posts: 37
Hi all,

I have to customize JButton in my application.

Default gray button is not what I want...

I have downloaded an image from internet and I want to put this image as background for my component.

I have try in paint method, by using ImageIcon, but I have 2 problems:

Image is not a background, but it is placed to the left of the button

Image is not resizable.

I simply want that the image become the background of the button, and that the text of the button is placed over the image. (As a simple "EXIT" button)

How can I do that?

Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

Adriano, you are partly on the right track, by using an ImageIcon object made from your image. But you don't need to do anything in your Paint method. Read the JButton methods documentation. You will find one that lets you assign your ImageIcon to the JButton, doing most of what you want. Your JButton's text will not (at least not easily) appear over the image, but a simple workaround would be to put the text into the image itself.

If you can't get that to work, try posting enough of your code so people here can help you with it.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Stevens Miller wrote:Your JButton's text will not (at least not easily) appear over the image, but a simple workaround would be to put the text into the image itself.

No need for that. Just setHorizontal/VerticalTextPosition appropriately.


luck, db
There are no new questions, but there may be new answers.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Adriano Bellavita wrote:Hi all,

I have to customize JButton in my application.

Default gray button is not what I want...

I have downloaded an image from internet and I want to put this image as background for my component.

I have try in paint method, by using ImageIcon, but I have 2 problems:

Image is not a background, but it is placed to the left of the button

Image is not resizable.

I simply want that the image become the background of the button, and that the text of the button is placed over the image. (As a simple "EXIT" button)

How can I do that?



There's tutorial linked from the API for JButton wehre you can find the answers to all this and more.

edit Except tha part about a resizable image. For that you may need to write your own Icon implementation, and ensure that the layout takes care of determining the size of the button.
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

Darryl Burke wrote:
Stevens Miller wrote:Your JButton's text will not (at least not easily) appear over the image, but a simple workaround would be to put the text into the image itself.

No need for that. Just setHorizontal/VerticalTextPosition appropriately.

Ah, right you are:

Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JButton background in SWING
 
Similar Threads
Round Buttons in Java
How to place image on JButton
Image in JLabel
Component on Image
how do i create a customized button using a graphic image