I'm trying to change the color when I mouse over a cell on a combo box. Do I use a mouse event for that or just this cell renderer?
In what way do I change the color of JComboBox
cell on mouse hover?
Here's my code:
jComboBox2.setRenderer(new DefaultListCellRenderer() {
public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (isSelected){
c.setBackground(new Color(242,242,242));
}
if(index == 0){
c.setForeground(new Color(242,242,242));
c.setBackground(new Color(26,35,48));
}
if(index == 1){
c.setForeground(new Color(101,150,226));
c.setBackground(new Color(26,35,48));
}
if(index == 2){
c.setForeground(new Color(77,105,205));
c.setBackground(new Color(26,35,48));
}
if(index == 3){
c.setForeground(new Color(137,71,255));
c.setBackground(new Color(26,35,48));
}
if(index == 4){
c.setForeground(new Color(212,43,230));
c.setBackground(new Color(26,35,48));
}
if(index == 5){
c.setForeground(new Color(235,75,75));
c.setBackground(new Color(26,35,48));
}
else {
}
return c;
}
});
I have found the answer
I just have to nest if to change the color on hover
Like this
if(index == 0){
c.setForeground(new Color(242,242,242));
c.setBackground(new Color(26,35,48));
if (isSelected) {
c.setBackground(new Color(49,62,80));
}
}
Question: it should also change the foreground color of the combobox. jComboBox2.setForeground(new Color(211,211,211)); inside if(isSelected){} wont work
Place the if(isSelected){c.setForeground(new Color(211,211,211));} at end of else statement so the we can override color..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.