1

I'm always intrigued by different GUIs and seeing custom components on them. At the moment, I'm curious about making customized JButtons.

For an example of what I'm referring to, look at this GUI (this was just a quick one I pulled up):

enter image description here

All of the buttons are customized. I'm wondering what is the proper/accepted way to do these buttons.

Personally, when I want to create a custom button, I simply do something like:

ImageIcon icon = new ImageIcon(getClass().getClassLoader().getResource(resourcePath));
Image img = icon.getImage();
Image newimg = img.getScaledInstance(buttonW, buttonH, java.awt.Image.SCALE_SMOOTH);
button.setIcon(new ImageIcon(newimg));
button.setMargin(new Insets(0,0,0,0));
button.setBorderPainted(false);  
button.setFocusPainted(false);  
button.setContentAreaFilled(false);  
button.setText("");

This seems like the easy way to do it, but I'm not sure if it is the proper way to do it. It preserves the JButton functionality, but not necessarily the ability to customize it. Basically, I would be stuck the background of whatever image I have. I would also only be able to make the button appear to have rounded corners.

Is the above code an improper way of doing this? I feel it probably is. If so, can you provide other ways that I should be doing it?

I've run across a few nice examples, such as:

Anyway, I'm just looking to see how people create their custom JButtons and perhaps what is the more acceptable way to do it when in a work environment.

If you have any images of your custom buttons, I would enjoy viewing them and perhaps the source you used to create them.

Community
  • 1
  • 1
WilliamShatner
  • 926
  • 2
  • 12
  • 25
  • 5
    Well, if you want to make really high detailed and nice-looking buttons (or any component for that matter) I would recommend using JavaFX, as it is very flexible with design. – Josh M Aug 15 '13 at 18:24
  • Are you talking about LAF? – nanofarad Aug 15 '13 at 18:29
  • @JoshM That is pretty neat. Essentially it is using an external library? It looks like it may be worth looking into. Thanks for the heads up. – WilliamShatner Aug 15 '13 at 18:29
  • @hexafraction I suppose it could be listed as that. But I've looked into a lot of look & feel libraries. I personally would just like to create my own nice looking buttons. (Rounded edges, change color with a simple call, change icon/image with a call, etc) - But I want to do it the correct way, not some hack-job like I feel I'm currently doing. – WilliamShatner Aug 15 '13 at 18:31
  • Well, if you are in to high quality pre-designed LAFs then you should probably use [substance and trident](http://www.pushing-pixels.org/). And if you are using a JDK version of atleast 7, JavaFX is bundled in already. – Josh M Aug 15 '13 at 18:34
  • 1
    So essentially I would be wasting my time (reinventing the wheel) creating my own buttons? I'm currently at JDK6, perhaps I should just upgrade. Do you mind showing me some of the buttons you've done via JavaFX? - **Edit** Nevermind, found a nice little site! http://www.jgandrews.com/?p=143 – WilliamShatner Aug 15 '13 at 18:42

1 Answers1

1

You may want to write a custom Button UI, you can extend javax.swing.plaf.ComponentUI.

You can apply a custom UI to only one button, or a few buttons, or you create your custom Look and Feel, so all Button use your UI.

The advanced of creating a custom UI class instead of using images is

  • Scalable
  • You are able to translate texts
  • Less memory usage, you don't have to load a big image per button

For UI Examples see e.g. http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/swing/plaf/metal/MetalButtonUI.java

If you only need one or two buttons, using an image is may the best solution for you.

Andreas B.
  • 89
  • 2
  • Interesting, between you and Josh, you guys have given me enough incentive to look into JavaFX. I'll start reading up. Currently, I only need a handful of custom buttons, but eventually that list could grow. I thought for sure someone would come along and post an example of their custom drawn button, perhaps I was a bit too optimistic though! – WilliamShatner Aug 15 '13 at 18:46