Java – the color of the drop-down control and border in the non editable jcombobox

Non editable JCombo@R_817_2419 @The background color selected in is a blue color:

I know you can change it to another color, such as white, for example, using following code:

jCombo@R_817_2419@1.setRenderer(new DefaultListCellRenderer() {
    @Override
    public void paint(Graphics g) {
        setBackground(Color.WHITE);
        setForeground(Color.BLACK);
        super.paint(g);
    }
});

This gives you something like this:

However, if you double-click the combo box, some of them turn gray (parts with triangles and borders):

When you double-click it, is there any way to prevent these parts from graying out?

Note that if super. Is called first Paint (), the whole thing will darken (including the part after "select..."), which is useless

Solution

A few things:

>The appearance of the combo box (display area, arrow, drop-down) depends on the LAF Your screenshot suggests using WinXP If you have to support any other LAF, be sure to test it because what works for one LAF may not work for another I found that for JCombo@R_817_2419 @This is especially true for es. > As twister suggests, overriding the paint () method to change the color may not be the best way Just set the background / foreground color of the combo box itself If you want to change the color of the drop-down list itself (I don't know if you want to do this), then add a custom renderer to override getlistcellrendercomponent to set the background / foreground

public static class CustomRenderer extends DefaultListCellRenderer {

@Override
public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus) {
    super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus);
    setBackground(Color.WHITE);
    setForeground(Color.BLACK);     
    return this;
}

}> the appearance of gray triangles and borders is because the combo box now has focus You can make it unfocused and the coloring will disappear However, this may not be the behavior you want

JCombo@R_817_2419@ combo = new JCombo@R_817_2419@(new Object[]{"Dog","Cat","Bird"});
combo.setBackground(Color.WHITE);
combo.setForeground(Color.BLACK);
combo.setFocusable(false);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>