• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JButton background in SWING

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic