【问题标题】:Changing the color of the selected value in an uneditable jcombobox在不可编辑的 jcombobox 中更改所选值的颜色
【发布时间】:2013-01-04 18:01:36
【问题描述】:

如何在不可编辑的 jcombobox 中更改所选值的颜色?

我知道我必须使用自定义渲染器来更改出现在组合框下拉列表中的项目的颜色。我也知道可以与组合框的编辑器组件一起使用的 setForeground() 方法,但这仅适用于可编辑的组合框。我也知道我可以使用 UIManager 类,但这会影响全局的这些属性。

但是,这些不是我需要的。这张图片

显示我想要影响的部分。

【问题讨论】:

    标签: java swing jcombobox


    【解决方案1】:

    如何在不可编辑的文件中更改所选值的颜色 jcombobox?

    • 更改UIManager 的值

    这里是可编辑的基本解决方法(删除背景到 当前背景,注意 MetalComboBoxButton 中有快捷方式, 然后是外观和感觉敏感,适用于金属和 仅物质 L&f)

    import java.awt.*;
    import java.util.Vector;
    import javax.swing.*;
    import javax.swing.UIManager;
    import javax.swing.plaf.ColorUIResource;
    import javax.swing.plaf.metal.MetalComboBoxButton;
    
    public class MyComboBox {
    
        private Vector<String> listSomeString = new Vector<String>();
        private JComboBox someComboBox = new JComboBox(listSomeString);
        private JComboBox editableComboBox = new JComboBox(listSomeString);
        private JComboBox non_EditableComboBox = new JComboBox(listSomeString);
        private JFrame frame;
    
        public MyComboBox() {
            //for (int i = 0; i < 100000; i++) {
                listSomeString.add("-");
                listSomeString.add("Snowboarding");
                listSomeString.add("Rowing");
                listSomeString.add("Knitting");
                listSomeString.add("Speed reading");
            //}
    //
            someComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            someComboBox.setFont(new Font("Serif", Font.BOLD, 16));
            someComboBox.setEditable(true);
            someComboBox.getEditor().getEditorComponent().setBackground(Color.YELLOW);
            ((JTextField) someComboBox.getEditor().getEditorComponent()).setBackground(Color.YELLOW);
    //
            editableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            editableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
            editableComboBox.setEditable(true);
            JTextField text = ((JTextField) editableComboBox.getEditor().getEditorComponent());
            text.setBackground(Color.YELLOW);
            JComboBox coloredArrowsCombo = editableComboBox;
            Component[] comp = coloredArrowsCombo.getComponents();
            for (int i = 0; i < comp.length; i++) {// hack valid only for Metal L&F
                if (comp[i] instanceof MetalComboBoxButton) {
                    MetalComboBoxButton coloredArrowsButton = (MetalComboBoxButton) comp[i];
                    coloredArrowsButton.setBackground(null);
                    break;
                }
            }
    //
            non_EditableComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            non_EditableComboBox.setFont(new Font("Serif", Font.BOLD, 16));
    //
            frame = new JFrame();
            frame.setLayout(new GridLayout(0, 1, 10, 10));
            frame.add(someComboBox);
            frame.add(editableComboBox);
            frame.add(non_EditableComboBox);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocation(100, 100);
            frame.pack();
            frame.setVisible(true);
    
            //System.out.println(listSomeString.size());
            //System.out.println(someComboBox.getItemCount());
            //System.out.println(editableComboBox.getItemCount());
            //System.out.println(non_EditableComboBox.getItemCount());
        }
    
        public static void main(String[] args) {
            UIManager.put("ComboBox.background", new ColorUIResource(Color.yellow));
            UIManager.put("JTextField.background", new ColorUIResource(Color.yellow));
            UIManager.put("ComboBox.selectionBackground", new ColorUIResource(Color.magenta));
            UIManager.put("ComboBox.selectionForeground", new ColorUIResource(Color.blue));
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    MyComboBox aCTF = new MyComboBox();
                }
            });
        }
    }
    

    【讨论】:

    • 我的问题是,这个解决方案只会改变不可编辑组合框的选定值的颜色,如果它是字面上选择的(在选择一个值之后)并且如果它失去焦点,它的颜色会变回来。此外,这会影响所有组合框,我只想更改特定组合框的颜色。
    • @József Legoza 没有清除您的评论,需要详细说明,需要在屏幕上查看,选择了哪个项目,不要混淆用户....
    • 我想将所选值的文本颜色(选择的意思是下拉箭头左侧的部分)设置为黑色以外的任何颜色。如果我使用 UIManger,所有组合框都将具有相同的属性,但我只想更改特定组合框的颜色。在您的示例中尝试以下操作:在第三个组合框中选择一个值,然后单击其他组合框之一。第三个组合框的文本颜色从蓝色切换回黑色。在这种情况下,我也希望文本颜色保持蓝色。
    • 我应该使用什么渲染器?我尝试使用自定义 ListCellRenderer,但这仅适用于下拉列表,它不会影响所选值的颜色(箭头旁边的那个)。
    • 你到底是什么意思?我应该在哪里/如何覆盖它们?我尝试在“getListCellRendererComponent”中使用“isSelected”和“hasFocus”,但这些是参数,我无法覆盖它们,所以这可能不是你的想法。
    【解决方案2】:

    使组合框可编辑

    获取组合框的编辑器组件

    将 editorComponent 设置为可聚焦(false)

    不确定这是您所追求的,因为您仍然可以更改所选项目,但无法专注于 editorComponent 来更改项目。

    【讨论】:

    • 我想更改颜色而不使组合框可编辑。
    • @Michael Dunn 然后也更改了按钮背景,用于可编辑的 JComboBox
    猜你喜欢
    • 2012-05-02
    • 2011-05-23
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多