• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JButton background in SWING

 
Adriano Bellavita
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1261
24
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1261
24
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic