/Users/joshy/projects/current/swingx/src/java/org/jdesktop/swingx/JXColorSelectionButton.java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.jdesktop.swingx;
23
24 import java.awt.Color;
25 import java.awt.Graphics;
26 import java.awt.Insets;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.awt.image.BufferedImage;
30 import javax.imageio.ImageIO;
31 import javax.swing.JButton;
32 import javax.swing.JColorChooser;
33 import javax.swing.JComponent;
34 import javax.swing.JDialog;
35 import javax.swing.JFrame;
36 import javax.swing.JLabel;
37 import javax.swing.JPanel;
38 import javax.swing.event.ChangeEvent;
39 import javax.swing.event.ChangeListener;
40 import org.jdesktop.swingx.color.*;
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class JXColorSelectionButton extends JButton {
55 private BufferedImage colorwell;
56 private JDialog dialog = null;
57 private JColorChooser chooser = null;
58
59
60
61
62 public JXColorSelectionButton() {
63 this(Color.red);
64 }
65
66
67
68
69
70 public JXColorSelectionButton(Color col) {
71 setBackground(col);
72 this.addActionListener(new ActionHandler());
73 this.setContentAreaFilled(false);
74 this.setOpaque(false);
75
76 try {
77 colorwell = ImageIO.read(this.getClass().getResourceAsStream("/org/jdesktop/swingx/color/colorwell.png"));
78 } catch (Exception ex) {
79 ex.printStackTrace();
80 }
81 }
82
83
84
85
86
87
88 private class ColorChangeListener implements ChangeListener {
89 public JXColorSelectionButton button;
90 public ColorChangeListener(JXColorSelectionButton button) {
91 this.button = button;
92 }
93 public void stateChanged(ChangeEvent changeEvent) {
94 button.setBackground(button.getChooser().getColor());
95 }
96 }
97
98
99
100
101 protected void paintComponent(Graphics g) {
102
103 Insets ins = new Insets(5,5,5,5);
104 if(colorwell != null) {
105 ColorUtil.tileStretchPaint(g, this, colorwell, ins);
106 }
107
108
109 g.setColor(ColorUtil.removeAlpha(getBackground()));
110 g.fillRect(ins.left, ins.top,
111 getWidth() - ins.left - ins.right,
112 getHeight() - ins.top - ins.bottom);
113
114 g.setColor(ColorUtil.setBrightness(getBackground(),0.85f));
115 g.drawRect(ins.left, ins.top,
116 getWidth() - ins.left - ins.right - 1,
117 getHeight() - ins.top - ins.bottom - 1);
118 g.drawRect(ins.left + 1, ins.top + 1,
119 getWidth() - ins.left - ins.right - 3,
120 getHeight() - ins.top - ins.bottom - 3);
121 }
122
123
124
125
126
127
128 public static void main(String[] args) {
129 JFrame frame = new JFrame("Color Button Test");
130 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
131 JPanel panel = new JPanel();
132 panel.add(new JXColorSelectionButton());
133 panel.add(new JLabel("ColorSelectionButton test"));
134
135 frame.add(panel);
136 frame.pack();
137 frame.setVisible(true);
138 }
139
140
141
142
143 private void showDialog() {
144 if (dialog == null) {
145 dialog = JColorChooser.createDialog(JXColorSelectionButton.this,
146 "Choose a color", true, getChooser(),
147 new ActionListener() {
148 public void actionPerformed(ActionEvent actionEvent) {
149 }
150 },
151 new ActionListener() {
152 public void actionPerformed(ActionEvent actionEvent) {
153 }
154 });
155 dialog.getContentPane().add(getChooser());
156 getChooser().getSelectionModel().addChangeListener(
157 new ColorChangeListener(JXColorSelectionButton.this));
158 }
159 dialog.setVisible(true);
160 Color color = getChooser().getColor();
161 if (color != null) {
162 setBackground(color);
163 }
164 }
165
166
167
168
169
170
171
172 public JColorChooser getChooser() {
173 if(chooser == null) {
174 chooser = new JColorChooser();
175
176 chooser.addChooserPanel(new EyeDropperColorChooserPanel());
177 }
178 return chooser;
179 }
180
181
182
183
184
185
186
187
188 public void setChooser(JColorChooser chooser) {
189 JColorChooser oldChooser = getChooser();
190 this.chooser = chooser;
191 firePropertyChange("chooser",oldChooser,chooser);
192 }
193
194
195
196
197
198 private class ActionHandler implements ActionListener {
199
200 public void actionPerformed(ActionEvent actionEvent) {
201 showDialog();
202 }
203 }
204
205
206 }
207