Java Reference
In-Depth Information
// Given a label, find the position of the label in the list
private int findColorLabel(Object label) {
String stringLabel = label.toString();
int position = NOT_FOUND;
for (int i=0,n=labels.length; i<n; i++) {
if (stringLabel.equals(labels[i])) {
position=i;
break;
}
}
return position;
}
// Given a color, find the position whose color matches
// This could result in a position different from original if two are equal
// Since actual color is same, this is considered to be okay
private int findColorPosition(Color color) {
int position = colors.length-1;
// Cannot use equals() to compare Color and SystemColor
int colorRGB = color.getRGB();
for (int i=0,n=colors.length; i<n; i++) {
if ((colors[i] != null) && (colorRGB == colors[i].getRGB())) {
position=i;
break;
}
}
return position;
}
public void itemStateChanged(ItemEvent itemEvent) {
int state = itemEvent.getStateChange();
if (state == ItemEvent.SELECTED) {
int position = findColorLabel(itemEvent.getItem());
// last position is bad (not selectable)
if ((position != NOT_FOUND) && (position != labels.length-1)) {
ColorSelectionModel selectionModel = getColorSelectionModel();
selectionModel.setSelectedColor(colors[position]);
}
}
}
public String getDisplayName() {
return "SystemColor";
}
Search WWH ::




Custom Search