Bonjour,

Dans l'exemple qui suit, j'affiche un combo dont je colorie chaque partie avec des couleurs différentes ceci sans problème en mode setEdit(true).

Par contre impossible de colorier la partie ""zone d'entrée" avec setEdit(false) (car je ne dois pas donner la possibilité de modifier le texte).

Si vous avez des idées merci d'avance.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
import javax.swing.*;
import java.awt.*;
 
public class CustomComboBox
{
	public static void main(String[] args)
	{
		String[] items = {  "Bananes", "Cerises", "Oranges", "Pommes" };
 
		JFrame frame = new JFrame("JComboBox Custom Colors");
			frame.setSize(213, 200);
			frame.getContentPane().setLayout(null);
 
		JButton Finish = new JButton("Fin");
			Finish.addActionListener(e -> System.exit(0));
			Finish.setBounds(43, 133, 100, 23);
 
		JComboBox<String> comboBox = new JComboBox<>(items);
			comboBox.setBounds(43, 23, 101, 20);
			comboBox.setRenderer(new DefaultListCellRenderer()
			{
				private static final long serialVersionUID = 1L;
				@Override
				public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
					boolean cellHasFocus)
				{
					Component renderer = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
					renderer.setForeground(Color.BLACK);
					renderer.setBackground(Color.pink);
					return renderer;
				}
			});
 
			JPanel panel = new JPanel();
				panel.setLayout(null);
				panel.setBounds(10, 10, 193, 179);
				panel.setBackground(new Color(102, 205, 170));
				panel.add(comboBox);
				panel.add(Finish);
 
			frame.getContentPane().add(panel);
 
		// coloring  the entry field
		ComboBoxEditor cedit = comboBox.getEditor();
		JTextField texte = (JTextField) cedit.getEditorComponent();
		texte.setBackground(Color.green);
		texte.setForeground(Color.red);
		cedit.setItem(texte);
		comboBox.setEditor(cedit);
 
		/*   if je change  par  "false" la partie texte n'est plus coloriée mais le reste l'est toujours.   */
		comboBox.setEditable(true); 
                // comboBox.setEditable(false); 
		/*                                                                          */
 
		frame.setUndecorated(true);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
}