/Users/joshy/projects/current/swingx/src/java/org/jdesktop/swingx/JXColorSelectionButton.java |
package org.jdesktop.swingx;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jdesktop.swingx.color.*;
public class JXColorSelectionButton extends JButton {
private BufferedImage colorwell;
private JDialog dialog = null;
private JColorChooser chooser = null;
public JXColorSelectionButton() {
this(Color.red);
}
public JXColorSelectionButton(Color col) {
setBackground(col);
this.addActionListener(new ActionHandler());
this.setContentAreaFilled(false);
this.setOpaque(false);
try {
colorwell = ImageIO.read(this.getClass().getResourceAsStream("/org/jdesktop/swingx/color/colorwell.png"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
private class ColorChangeListener implements ChangeListener {
public JXColorSelectionButton button;
public ColorChangeListener(JXColorSelectionButton button) {
this.button = button;
}
public void stateChanged(ChangeEvent changeEvent) {
button.setBackground(button.getChooser().getColor());
}
}
protected void paintComponent(Graphics g) {
Insets ins = new Insets(5,5,5,5);
if(colorwell != null) {
ColorUtil.tileStretchPaint(g, this, colorwell, ins);
}
g.setColor(ColorUtil.removeAlpha(getBackground()));
g.fillRect(ins.left, ins.top,
getWidth() - ins.left - ins.right,
getHeight() - ins.top - ins.bottom);
g.setColor(ColorUtil.setBrightness(getBackground(),0.85f));
g.drawRect(ins.left, ins.top,
getWidth() - ins.left - ins.right - 1,
getHeight() - ins.top - ins.bottom - 1);
g.drawRect(ins.left + 1, ins.top + 1,
getWidth() - ins.left - ins.right - 3,
getHeight() - ins.top - ins.bottom - 3);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Color Button Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.add(new JXColorSelectionButton());
panel.add(new JLabel("ColorSelectionButton test"));
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
private void showDialog() {
if (dialog == null) {
dialog = JColorChooser.createDialog(JXColorSelectionButton.this,
"Choose a color", true, getChooser(),
new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
}
},
new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
}
});
dialog.getContentPane().add(getChooser());
getChooser().getSelectionModel().addChangeListener(
new ColorChangeListener(JXColorSelectionButton.this));
}
dialog.setVisible(true);
Color color = getChooser().getColor();
if (color != null) {
setBackground(color);
}
}
public JColorChooser getChooser() {
if(chooser == null) {
chooser = new JColorChooser();
chooser.addChooserPanel(new EyeDropperColorChooserPanel());
}
return chooser;
}
private class ActionHandler implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
showDialog();
}
}
}