簡體   English   中英

在沒有渲染器的情況下更改JComboBox顏色

[英]Change JComboBox colours WITHOUT renderer

我可以使用以下方法更改ComboBox背景顏色:

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

而且有效。

但是要更改[selected] .background ,請查看“ Nimbus默認值”,該屬性稱為ComboBox:“ ComboBox.listRenderer” [Selected] .background ,因此我嘗試使用:

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

但這不起作用。

我想用一個渲染器來做到這一點(我已經嘗試過了,並且把很多問題都交給了我自己沒有寫過的很長的代碼,如果我這樣做的話,將組合框渲染到JFileChoosers中是一個額外的問題)。 因此,是否有解決方案可使用UIMAnager.put()修復此問題?

設置不同的Color ,而不使用Nimbus defaluts

1 /用於單獨的JComboBox

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

2 /用於JFileChooser

  • 此處提取的JFileChooser (復合JComponents )中提取所有JComponents ,方法與針對JListJScrooPane所述的方法相同

  • 通過提取safiest方式,所有JComponentsJFileChooser的建議在以前的帖子關於這里

3 /通過使用NimbusDefalut查找默認值

  • JTextField並按照我的建議1

  • JComboBox's DropDown ListJComboBox's DropDown ListJList JComboBox's DropDown List ,從JTable選擇的HighLighter的JComboBox's DropDown List

編輯:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM