Drop-down control color and border in unedited JComboBox

The background color of the selected item in an unedited JComboBox is kind of blue:

alt text

I know that you can change it to a different color, for example white, for example with the following code :

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

This will give you something like this:

alt text

However, if you double-click this combo box, some of them will turn gray (part with a triangle and a border):

alt text

Is there a way to prevent these parts from getting gray when you double-click on it?

Note that if you call super.paint () first, everything becomes dark (including the part behind "Select ..."), so this does not help.

+4
source share
3 answers

A few things:

  • The appearance of the drop-down list (display area, arrow, drop-down) depends on LAF. Your screenshots offer WinXP. If you must support any other LAFs, be sure to check this out because what works for one LAF may not work for another. I found this to be especially true for JComboBoxes.

  • As Twister says, changing the color by overriding the paint () method is probably not the best way to do this. Just set the background / foreground color in the combo box. If you want to change the color of the drop-down list itself (I don’t understand if you want to do this or not), add a custom renderer that overrides getListCellRendererComponent 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 a gray triangle and border is due to the fact that the combo box now has focus. You can just make it non-orientable and the coloring will disappear. However, this may not be the behavior you want.

     JComboBox combo = new JComboBox(new Object[]{"Dog", "Cat", "Bird"}); combo.setBackground(Color.WHITE); combo.setForeground(Color.BLACK); combo.setFocusable(false); 
+1
source

First, you should not set the foreground and background in the drawing method. To configure it, you must override the getListCellRendererComponent of your renderer. The default rendering tool changes its aspect if you have focus or it is selected. If you do not want these functions to reuse this method.

Then, if you add a line border to your renderer (setBorder (BorderFactory.createLineBorder (Color.black)), you will see that what is drawn is not part of your renderer, but its self-image. You may need to configure Custom interface

0
source

The coding code that calls getListCellRendererComponent then calls setForeground and setBackground on the returned component (depending on whether the component is selected and / or focused). I guess this is for some kind of legacy. Unfortunately, this defeats the purpose of my install in the renderer.

I had good results with this approach:

The code below bypasses the changes in the foreground and background by overriding the fg / bg setters to do nothing, then I just call the super implementations to set the desired colors.

 public static class CustomRenderer extends DefaultListCellRenderer { public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); super.setBackground(Color.WHITE); super.setForeground(Color.BLACK); return this; } public void setForeground(Color c) {} public void setBackground(Color c) {} } 

Addendum: Obviously, the gray border is the border. Try the same approach, but also override setBorder .

0
source

Source: https://habr.com/ru/post/1333730/


All Articles