import java.awt.HeadlessException; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JColorChooser; import javax.swing.JDialog; import javax.swing.JPanel; import javax.swing.JButton; /** * ColorPanel creates a JColorChooser and allows the user to choose a new color * to change with the selected swatch. **/ public class ColorPanel extends JPanel implements ActionListener { private JColorChooser jcc; private JDialog jccHolder; /** Creates a default ColorBox. **/ public ColorPanel() { // instantiates the JColorChooser and sets it visible. jcc = new JColorChooser(); try{ jccHolder = jcc.createDialog(this, "Color Panel", false, jcc, this, this); } catch(HeadlessException he){} } /** Sets the JDialog's visible attribute true or false. **/ public void setVisible(boolean visible) { jccHolder.setVisible(visible); } /** Changes the color you selected with the last selected swatch. **/ public void actionPerformed(ActionEvent e) { // if the okay button is clicked then change the selected swatch and // the left mouse click color JButton button = (JButton)e.getSource(); if(button.getText() == "OK") { ColorBox.colorPal[ColorBox.selectedSwatchX][ColorBox.selectedSwatchY].setBackground(jcc.getColor()); ColorBox.foreGround.setBackground(jcc.getColor()); } } }