1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| public class ChoicePanelGUI extends JPanel {
private static final long serialVersionUID = 1L;
private JComboBox comboTitleTextColor;
private GridBagConstraints gbc = new GridBagConstraints();
// Le composant qui affiche une case de la liste lorsque c'est une couleur
static class ColorRenderer extends JComponent {
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g) {
g.setColor(this.getBackground());
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(this.getForeground());
g.fillRect(10, 10, this.getWidth() - 20, this.getHeight() - 20);
}
}
public ChoicePanelGUI(){
//TODO Auto-generated constructor stub
super(new GridBagLayout());
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(0,0,10,0);
final Color [] myColor;
myColor = new Color[7];
myColor[0] = Color.black;
myColor[1] = Color.white;
myColor[2] = Color.blue;
myColor[3] = Color.green;
myColor[4] = Color.gray;
myColor[5] = Color.yellow;
myColor[6] = Color.pink;
comboTitleTextColor = new JComboBox();
comboTitleTextColor.setOpaque(true);
for(int i = 0; i < myColor.length; i++){
comboTitleTextColor.addItem(myColor[i]);
}
comboTitleTextColor.setRenderer(new ColorListCellRenderer(){
ColorRenderer cr = new ColorRenderer();
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
if(value instanceof Color) {
if(isSelected){
cr.setBackground(Color.red);
}else{
cr.setBackground(Color.WHITE);
cr.setForeground((Color)value);
}
return cr;
}
else
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
}
});
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 1;
gbc.gridy = 0;
this.add(comboTitleTextColor,gbc); |
Partager