无需渲染器即可更改 JComboBox 颜色

发布于 2024-11-26 14:27:38 字数 650 浏览 2 评论 0原文

我可以使用以下命令更改组合框背景颜色:

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

并且它有效。

但要更改[selected].background,请查看Nimbus Defaults 该属性称为 ComboBox:"ComboBox.listRenderer"[Selected].background,所以我尝试使用:

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

但是它不起作用。

我想用渲染器来做到这一点(我已经尝试过,并且在我什至没有自己编写的长代码中给出了许多问题,并且如果我这样做,将组合框渲染到 JFileChoosers 中是一个额外的问题)。那么,是否有任何解决方案可以使用 UIMManager.put() 来解决此问题?

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()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自找没趣 2024-12-03 14:27:38

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

1/ 对于单独的 JComboBox

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

2/ 对于 JFileChooser

  • JFileChooser< 中提取所有 JComponents /code>(复合JComponents)如建议的此处,与 JList 描述的方式相同JScrooPane

  • 最安全的方法是从 JFileChooser 中提取所有 JComponents,正如您之前关于 这里

3/ 通过使用 NimbusDefalut 查找默认值

  • JTextField 以及我的添加 No.1 中的建议

  • JComboBox 的下拉列表 来自 JList 的默认值,HighLighter 用于从 JTable

    中进行选择

编辑:

代码

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

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();
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文