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):
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:
- Creating a separate class that extends JButton - Answer 1 & 2
- Making sure the button works only when the button is clicked (not the invisible pixels)
- Creating a custom JPanel and perhaps adding listeners to it?
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.