aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes how do i create a customized button using a graphic image 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 do i create a customized button using a graphic image " Watch "how do i create a customized button using a graphic image " New topic
Author

how do i create a customized button using a graphic image

aity myles
Greenhorn

Joined: Jun 14, 2011
Posts: 13
i dont want to make use of the regular JButton with an image in it in my application. i want to make use of a graphic image button (custom button) in my application. so i am wondering if it is possible to do that in java?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2318
    
  49
Yes, basically you can create your own button class which extends JButton and provide your own implementation of the paintComponent(Graphics) method.
However if you just want a standard button but displaying an image rather than text JButton will do this for you. Use the JButton constructor that takes an ImageIcon.
aity myles
Greenhorn

Joined: Jun 14, 2011
Posts: 13
Tony Docherty wrote:Yes, basically you can create your own button class which extends JButton and provide your own implementation of the paintComponent(Graphics) method.
However if you just want a standard button but displaying an image rather than text JButton will do this for you. Use the JButton constructor that takes an ImageIcon.


thank you Tony. well i am wondering if using the first option you gave i.e extending the JButton class will work if am to make use of a spherical graphic image for the custom button.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2318
    
  49
If you want to change the shape of the button then you have no choice other than extend JButton and provide the means to paint the shape either through an image or by using the graphics object. Using the graphics object can be better if you need to be able to resize of the button as rescaling images can be expensive and depending on the button and image size the results may not look particularly good.

Some years ago I did a hexagonal button for a game project, the button code can be downloaded here . The button shape was drawn using the graphics object but it will give you an idea of what you need to do. The code also includes a layout manager for laying out hexagonal buttons which you can ignore.
aity myles
Greenhorn

Joined: Jun 14, 2011
Posts: 13
Thanks Tony. The solution you offered was perfect.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how do i create a customized button using a graphic image