/** This demonstrates how to customize a small <code>ColorPicker</code> component.
*/
public static void main(String[] args) {
final JFrame demo = new JFrame("Demo");
final JWindow palette = new JWindow(demo);
final ColorPicker picker = new ColorPicker(true,false);
final JComboBox comboBox = new JComboBox();
final JCheckBox alphaCheckbox = new JCheckBox("Include Alpha");
final JCheckBox hsbCheckbox = new JCheckBox("Include HSB Values");
final JCheckBox rgbCheckbox = new JCheckBox("Include RGB Values");
final JCheckBox modeCheckbox = new JCheckBox("Include Mode Controls",true);
final JButton button = new JButton("Show Dialog");
demo.getContentPane().setLayout(new GridBagLayout());
palette.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0; c.gridy = 0; c.weightx = 1; c.weighty = 0;
c.insets = new Insets(5,5,5,5); c.anchor = GridBagConstraints.WEST;
palette.getContentPane().add(comboBox,c);
c.gridy++;
palette.getContentPane().add(alphaCheckbox,c);
c.gridy++;
palette.getContentPane().add(hsbCheckbox,c);
c.gridy++;
palette.getContentPane().add(rgbCheckbox,c);
c.gridy++;
palette.getContentPane().add(modeCheckbox,c);
c.gridy = 0;
c.weighty = 1; c.fill = GridBagConstraints.BOTH;
picker.setPreferredSize(new Dimension(220,200));
demo.getContentPane().add(picker,c);
c.gridy++; c.weighty = 0;
demo.getContentPane().add(picker.getExpertControls(),c);
c.gridy++; c.fill = GridBagConstraints.NONE;
demo.getContentPane().add(button,c);
comboBox.addItem("Hue");
comboBox.addItem("Saturation");
comboBox.addItem("Brightness");
comboBox.addItem("Red");
comboBox.addItem("Green");
comboBox.addItem("Blue");
ActionListener checkboxListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if(src==alphaCheckbox) {
picker.setOpacityVisible(alphaCheckbox.isSelected());
} else if(src==hsbCheckbox) {
picker.setHSBControlsVisible(hsbCheckbox.isSelected());
} else if(src==rgbCheckbox) {
picker.setRGBControlsVisible(rgbCheckbox.isSelected());
} else if(src==modeCheckbox) {
picker.setModeControlsVisible(modeCheckbox.isSelected());
}
demo.pack();
}
};
picker.setOpacityVisible(false);
picker.setHSBControlsVisible(false);
picker.setRGBControlsVisible(false);
picker.setHexControlsVisible(false);
picker.setPreviewSwatchVisible(false);
picker.addPropertyChangeListener(ColorPicker.MODE_PROPERTY, new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
int m = picker.getMode();
if(m==ColorPicker.HUE) {
comboBox.setSelectedIndex(0);
} else if(m==ColorPicker.SAT) {
comboBox.setSelectedIndex(1);
} else if(m==ColorPicker.BRI) {
comboBox.setSelectedIndex(2);
} else if(m==ColorPicker.RED) {
comboBox.setSelectedIndex(3);
} else if(m==ColorPicker.GREEN) {
comboBox.setSelectedIndex(4);
} else if(m==ColorPicker.BLUE) {
comboBox.setSelectedIndex(5);
}
}
});
alphaCheckbox.addActionListener(checkboxListener);
hsbCheckbox.addActionListener(checkboxListener);
rgbCheckbox.addActionListener(checkboxListener);
modeCheckbox.addActionListener(checkboxListener);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Color color = picker.getColor();
color = ColorPicker.showDialog(demo, color, true);
if(color!=null)
picker.setColor(color);
}
});
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i = ((JComboBox)e.getSource()).getSelectedIndex();
if(i==0) {
picker.setMode(ColorPicker.HUE);
} else if(i==1) {
picker.setMode(ColorPicker.SAT);
} else if(i==2) {
picker.setMode(ColorPicker.BRI);
} else if(i==3) {
picker.setMode(ColorPicker.RED);
} else if(i==4) {
picker.setMode(ColorPicker.GREEN);
} else if(i==5) {
picker.setMode(ColorPicker.BLUE);
}
}
});
comboBox.setSelectedIndex(2);
palette.pack();
palette.setLocationRelativeTo(null);
demo.addComponentListener(new ComponentAdapter() {
@Override
public void componentMoved(ComponentEvent e) {
Point p = demo.getLocation();
palette.setLocation(new Point(p.x-palette.getWidth()-10,p.y));
}
});
demo.pack();
demo.setLocationRelativeTo(null);
demo.setVisible(true);
palette.setVisible(true);
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}