2

I can change the ComboBox background color using:

UIManager.put("ComboBox.background", Color.RED);

and it works.

But to change the [selected].background, having a look at Nimbus Defaults the property is called ComboBox:"ComboBox.listRenderer"[Selected].background, so I tried with:

UIManager.put("ComboBox:\"ComboBox.listRenderer\"[Selected].background", Color.RED);

but it doesn't work.

I want to do this with a renderer (which I have tried and gives many problems into a long code I even hadn't written myself, and rendering the comboboxes into the JFileChoosers is an extra problem if I go that way). So, is there any solution to fix this using UIMAnager.put()?

Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207

1 Answers1

1

set different Color, without using Nimbus defaluts

1/ for separate JComboBox

((JTextField) myJComboBox.getEditor().getEditorComponent())
#setBackground(Color.xxxx);

2/ for JFileChooser

  • extract all JComponents from JFileChooser (compound JComponents) as sugested here, same way as is described for JList and JScrooPane

  • safiest way by extract all JComponents from JFileChooser as suggested in your previous post about that here

3/ by using NimbusDefalut find defalut for

  • JTextField and as suggested in my add No.1

  • JComboBox's DropDown List from defaluts for JList, HighLighter for selection from JTable

EDIT:

code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboPopup;

public class DisabledEditableCombo extends JFrame {

    private static final long serialVersionUID = 1L;
    private String comboList[] = (new String[]{"-", "London", "New York", "Sydney", "Tokyo"});
    private JComboBox cmb = new JComboBox(comboList);
    private JComboBox cmb1 = new JComboBox(comboList);
    private JComboBox cmb2 = new JComboBox(comboList);
    private JComboBox cmb3 = new JComboBox(comboList);
    private JList list;
    private JCheckBox checkBox = new JCheckBox("Combo enabled", false);

    public DisabledEditableCombo() {
        JLabel lbl = new JLabel("Editable JComboBox");
        cmb.setEditable(true);
        ((JTextField) cmb.getEditor().getEditorComponent()).setDisabledTextColor(Color.red);
        ((JTextField) cmb.getEditor().getEditorComponent()).setBackground(Color.green);
        cmb.setSelectedItem("Just Editable");
        JLabel lbl1 = new JLabel("Non-Editable JComboBoxes");
        //UIManager.put("ComboBox.disabledForeground", Color.red.darker().darker());
        cmb1.setSelectedItem("Sydney");
        cmb1.setRenderer(new DefaultListCellRenderer() {//  ListCellRenderer

            private static final long serialVersionUID = 1L;

            @Override
            public void paint(Graphics g) {
                setBackground(cmb1.getBackground());
                setForeground(Color.red);
                super.paint(g);
            }
        });
        cmb2.getEditor().getEditorComponent().setForeground(Color.blue);
        ((JTextField) cmb2.getEditor().getEditorComponent()).setDisabledTextColor(Color.red);
        cmb2.setSelectedItem("London");
        cmb3.setSelectedItem("Sydney");
        checkBox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                boolean selected = checkBox.isSelected();
                cmb.setEnabled(selected);
                cmb1.setEnabled(selected);
                cmb2.setEnabled(selected);
                cmb2.setEditable(!cmb2.isEnabled());
                cmb2.setForeground(selected ? Color.blue : Color.red);
                if (cmb2.getEditor() != null) {
                    ((JTextField) cmb2.getEditor().getEditorComponent()).setDisabledTextColor(Color.red);
                }
                cmb3.setEnabled(selected);
                Object child = cmb3.getAccessibleContext().getAccessibleChild(0);
                BasicComboPopup popup = (BasicComboPopup) child;
                list = popup.getList();
                if (list != null) {
                    if (selected) {
                        list.setForeground(Color.blue);
                    } else {
                        list.setForeground(Color.red);
                    }
                }
            }
        });
        cmb.setEnabled(false);
        cmb1.setEnabled(false);
        cmb2.setEnabled(false);
        cmb3.setEnabled(false);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(300, 300));
        setLocation(150, 100);
        setLayout(new GridLayout(7, 0, 10, 10));
        add(lbl);
        add(cmb);
        add(lbl1);
        add(cmb1);
        add(cmb2);
        add(checkBox);
        add(cmb3);
        pack();
        setVisible(true);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                DisabledEditableCombo disabledEditableCombo = new DisabledEditableCombo();
            }
        });
    }
}
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • So far I made an attempt with solution #1, and i get an error. Changing the '#' for a '.' it goes, but nothing changes. Anyway that looked for changing the background, and not the SELECTED background – Roman Rdgz Jul 27 '11 at 12:53
  • Problem is 1st doesn't work, 2nd tells me to use code from my previous question which is supposed to fix only the JComboBox at the FileChooser but not the rest of them, and 3rd one I just don't understand what you are asking me to do. Finally, I also don't understand the code added after your edit: it is intended to replace JComboBox outside the FileChooser? Is it tested? – Roman Rdgz Jul 28 '11 at 07:31