Swing version of a Color property editor.
| Method from javax.swing.beaninfo.SwingColorEditor Detail: |
public void editorChangeValue(Object value) {
Color c = (Color)value;
if (c == null) {
rgbValue.setText(" ");
colorChooserCombo.setBackground(panel.getBackground());
return;
}
color = c;
// set the combo rect foreground color
// and the textfield to the rgb value
rgbValue.setText("" + c.getRed() + "," + c.getGreen() + "," + c.getBlue());
colorChooserCombo.setBackground(c);
}
|
public String getAsText() {
return rgbValue.getText();
}
|
public String getJavaInitializationString() {
return "new java.awt.Color(" + getAsText() + ")";
}
|
public SwingColorEditor getSwingColorEditor() {
return this;
}
|
public boolean isPaintable() {
return true;
}
|
public static void main(String[] argv) {
JFrame f = new JFrame();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
JFrame frame = (JFrame)e.getSource();
frame.dispose();
System.exit(0);
}
});
SwingColorEditor editor = new SwingColorEditor();
f.getContentPane().add(editor.getCustomEditor());
f.pack();
f.show();
}
|
public void paintValue(Graphics g,
Rectangle rect) {
Color oldColor = g.getColor();
g.setColor(Color.black);
g.drawRect(rect.x, rect.y, rect.width-3, rect.height-3);
g.setColor(color);
g.fillRect(rect.x+1, rect.y+1, rect.width-4, rect.height-4);
g.setColor(oldColor);
}
Paints a representation of the value into a given area of screen. |
public void setAsText(String s) throws IllegalArgumentException {
int c1 = s.indexOf(',");
int c2 = s.indexOf(',", c1+1);
if (c1 < 0 || c2 < 0) {
// Invalid string.
throw new IllegalArgumentException(s);
}
try {
int r = Integer.parseInt(s.substring(0,c1));
int g = Integer.parseInt(s.substring(c1+1, c2));
int b = Integer.parseInt(s.substring(c2+1));
setValue(new Color(r,g,b));
} catch (Exception ex) {
throw new IllegalArgumentException(s);
}
}
|
public void setValue(Object value) {
super.setValue(value);
editorChangeValue(value);
}
|