Salut!
Je suppose que clea doit exister, mais voici en tout cas une solution possible. A toi d'adapter...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.color.ColorSpace;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
/**
* Created on 15-avr.-07
*
* @author: HackTrack
*/
public class ComboColorChooser extends JComboBox {
private Component target;
private MyColor[] colors;
public ComboColorChooser(Component target) {
this.target = target;
initializeCombo();
}
private void initializeCombo() {
colors = new MyColor[3];
colors[0] = new MyColor(Color.RED.getRGB(), "Rouge");
colors[1] = new MyColor(Color.BLUE.getRGB(), "Bleu");
colors[2] = new MyColor(156, 58, 97, "Horrible");
DefaultComboBoxModel model = new DefaultComboBoxModel(colors);
setModel(model);
setRenderer(new ListCellRenderer() {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
System.out.println(value.toString());
MyColor myColor = (MyColor) value;
int w = 20;
int h = 20;
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.setColor(myColor);
g.fillRect(0, 0, w, h);
ImageIcon icon = new ImageIcon(img);
JLabel label = new JLabel(myColor.getColorName(), icon, JLabel.LEADING);
return label;
}
});
addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
JComboBox source = (JComboBox)e.getSource();
Color color = (MyColor)source.getSelectedItem();
target.setBackground(color);
target.repaint();
};
});
}
class MyColor extends Color {
private String colorName;
public MyColor(int rgb, String colorName) {
super(rgb);
this.colorName = colorName;
}
public MyColor(int rgba, boolean hasalpha, String colorName) {
super(rgba, hasalpha);
this.colorName = colorName;
}
public MyColor(int r, int g, int b, String colorName) {
super(r, g, b);
this.colorName = colorName;
}
public MyColor(float r, float g, float b, String colorName) {
super(r, g, b);
this.colorName = colorName;
}
public MyColor(ColorSpace cspace, float[] components, float alpha, String colorName) {
super(cspace, components, alpha);
this.colorName = colorName;
}
public MyColor(int r, int g, int b, int a, String colorName) {
super(r, g, b, a);
this.colorName = colorName;
}
public MyColor(float r, float g, float b, float a, String colorName) {
super(r, g, b, a);
this.colorName = colorName;
}
public String getColorName() {
return colorName;
}
}
public static void main(String[] args) {
JFrame view = new JFrame();
Container c = view.getContentPane();
c.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(320, 200));
c.add(panel, BorderLayout.CENTER);
c.add(new ComboColorChooser(panel), BorderLayout.NORTH);
view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
view.pack();
view.setVisible(true);
}
}
;-)
HackTrack