Customised JButtons

girma
girma used Ask the Experts™
on
hi,
  please help me to build customised JButtons of the different shapes like oval...
i read a thread wherein paint component was overwritten
but iam not getting how the class would be added to out panel without setting the bounds as rectangle.
any working example would be solicited
regards
giri
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Java Developer
Top Expert 2010
Commented:
Easiest way to customise a JButton is simply use an image to display it using the setIcon() method (plus various other setXXXIcon() methods for setting icons for different button states).

If for some reason you need to go further than that then you could supply your own custom L&F for your buttons.

Your customised component would still have rectangular bounds regardless of what shape it was painted as.

Hi,
the icon u initial on the first line is actually to button's look.
note that more complex buttons can be created by changing the displayed icon according to button's state (disable, click and hover) using mouse listener.

class CustomizeButton extends JButton {
   
    final static ImageIcon icon = new ImageIcon(<url, file name etc.>);
 
    public CustomizeButton() {
      super(icon);
      this.setBorder(null);
      this.setOpaque(false);
      this.setFocusPainted(false);
      this.setMargin(new Insets(0,0,0,0));
     
      Dimension dimension = new Dimension(this.getIcon().getIconHeight(), this.getIcon().getIconWidth());
      this.setPreferredSize(dimension);
      this.setMaximumSize(dimension);
    }
  }

-gkern
Mick BarryJava Developer
Top Expert 2010

Commented:
Yes, that does the same as using setIcon().
Not sure if it is necessary to set preferred and max sizes.
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

well,
i'm almost sure its necessary, anyway it won't harm.
also, it's very important to set border to null, to set focus painting to false and to set margin to zero in order to set a "clean" shape to the button.
girma:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Split points between objects and gkern

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Venabili
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial