• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how do i create a customized button using a graphic image

 
aity myles
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2988
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2988
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tony. The solution you offered was perfect.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic