(Java 1.6 required!)
The icons also brighten slightly when the mouse moves over them.
As several of these JCheckBoxes display the same icons I decided a nice solution would be to create some classes that override JCheckBox so I can easily make use of common settings and within an IDE (such as Netbeans) just drag and drop the widgets around.
I started by creating an AbstractIconCheckBox
This class provides a base with most of the logic in it to construct the widget. Note that it also provides a constructor which takes care of sizing the icons to be display.
package com.webbyit.swing;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
/**
* Abstract base <code>JCheckBox</code> which is intended to be used to display icons.
*
* @author webbst
*/
abstract public class AbstractIconCheckBox extends JCheckBox {
private static final int DEFAULT_ICON_SIZE = 16;
/**
* Creates a <code>JCheckBox</code> initially de-selected and displaying tick and cross icons
* of default size of 16 pixels height and width.
*/
public AbstractIconCheckBox() {
super();
setIcons(DEFAULT_ICON_SIZE);
}
/**
* Creates a <code>JCheckBox</code> initially de-selected and displaying tick and cross icons.
*
* @param iconSize the height and width of the tick and cross icons to be displayed
*/
public AbstractIconCheckBox(int iconSize) {
super();
setIcons(iconSize);
}
/**
* Set the icons on the <code>JCheckBox</code>
*/
protected void setIcons(int iconSize) {
try {
BufferedImage image = ImageUtilities.getBufferedImage(getSelectedIconName());
setSelectedIcon(new ImageIcon(ImageUtilities.createScaledImageFast(image, iconSize)));
image = ImageUtilities.getBufferedImage(getRolloverSelectedIconName());
setRolloverSelectedIcon(new ImageIcon(ImageUtilities.createScaledImageFast(image, iconSize)));
image = ImageUtilities.getBufferedImage(getIconName());
setIcon(new ImageIcon(ImageUtilities.createScaledImageFast(image, iconSize)));
image = ImageUtilities.getBufferedImage(getRolloverIconName());
setRolloverIcon(new ImageIcon(ImageUtilities.createScaledImageFast(image, iconSize)));
} catch (IOException ex) {
System.out.println("Icon not found in TickCrossCheckBox class" + ex);
}
}
abstract protected String getSelectedIconName();
abstract protected String getRolloverSelectedIconName();
abstract protected String getIconName();
abstract protected String getRolloverIconName();
}
All any implementing class then needs to do is to implement the abstract methods which provide the icons. As the TickCrossCheckBox demonstrates:
package com.webbyit.swing;
/**
* <code>JCheckBox</code> which displayed a tick or cross image instead of the normal box with a cross in it.
*
* @author webbst
*/
public class TickCrossCheckBox extends AbstractIconCheckBox {
/**
* Creates a <code>JCheckBox</code> initially de-selected and displaying tick and cross icons
* of default size of 16 pixels height and width.
*/
public TickCrossCheckBox() {
super();
}
/**
* Creates a <code>JCheckBox</code> initially de-selected and displaying tick and cross icons.
*
* @param iconSize the height and width of the tick and cross icons to be displayed
*/
public TickCrossCheckBox(int iconSize) {
super(iconSize);
}
@Override
protected String getSelectedIconName() {
return "tick_32.png";
}
@Override
protected String getRolloverSelectedIconName() {
return "tick_32_rollover.png";
}
@Override
protected String getIconName() {
return "cross-32.png";
}
@Override
protected String getRolloverIconName() {
return "cross-32_rollover.png";
}
}
I also have implementing classes for such things as sound mute checkboxes and so on.
I'm intending that my next post will look into developing an AutoComplete JComboBox. I've seen it done a few times before but I want to try something a little different. It should prove an interesting challenge and provide a useful component. I'll start with a teaser WebStart link and then write a few blogs about how I get it working.