开发者

Change JComboBox colours WITHOUT renderer

开发者 https://www.devze.com 2023-03-23 10:50 出处:网络
I can change the ComboBox background color using: UIManager.put(\"ComboBox.background\", Color.RED); and it works.

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 i开发者_开发技巧t 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()?


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();
            }
        });
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号