Java Reference
In-Depth Information
Note In addition, if you choose to override uninstallChooserPanel (JColorChooser
enclosingChooser) , you need to call super.uninstallChooserPanel (JColorChooser
enclosingChooser) last , instead of first.
When a user changes the color value in an AbstractColorChooserPanel , the panel must
notify the ColorSelectionModel of the change in color. In the SystemColorChooserPanel panel,
this equates to the user selecting a new choice in the JComboBox . Therefore, when the combo
box value changes, find the Color that equates to the choice and tell the model about the change.
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]);
}
}
}
The final AbstractColorChooserPanel method to implement is public void updateChooser() .
It, too, is called by installChooserPanel() at setup time. In addition, it's also called whenever
the ColorSelectionModel of the JColorChooser changes. When updateChooser() is called, the
chooser panel should update its display to show that the current color of the model is selected.
Not all panels show which color is currently selected, so a call may do nothing. (The system-
provided Swatches panel is one that doesn't display the current color.) In addition, it's possible that
the current color isn't displayable on the panel. For instance, on the SystemColorChooserPanel , if
the current selection isn't a SystemColor or Color constant, you can either do nothing or display
something to signify a custom color. Therefore, in the updateChooser() implementation, you
need to get the current color from the ColorSelectionModel and change the color for the panel.
The actual setting is done in a helper method called setColor(Color newValue) .
public void updateChooser() {
Color color = getColorFromModel();
setColor(color);
}
The setColor(Color newColor) method simply looks up the color in a lookup table using
the position returned from findColorPosition(Color newColor) .
// Change combo box to match color, if possible
private void setColor(Color newColor) {
int position = findColorPosition(newColor);
comboBox.setSelectedIndex(position);
}
 
Search WWH ::




Custom Search