如何更改JComboBox所选项的颜色?

时间:2012-12-02 05:42:21

标签: java swing user-interface jcombobox itemlistener

请查看以下代码

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class JCombo extends JFrame
{
    JComboBox com1,com2;

    public JCombo()
    {


        com1 = new JComboBox();
        com2 = new JComboBox();

        com1.addItem("One");
        com1.addItem("two");
        com1.addItem("Three");

        com2.addItem("Clothe");
        com2.addItem("Food");
        com2.addItem("Drinks");
        com2.addItemListener(new Com2Action());

        this.setLayout(new FlowLayout());
        this.add(com1);
        this.add(com2);

        this.pack();
        this.validate();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private class Com2Action implements ItemListener
    {
        public void itemStateChanged(ItemEvent ae)
        {
            if(ae.getStateChange() == ItemEvent.SELECTED)
            {
                com1.getSelectedItem();
            }
        }
    }

    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new JCombo();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }
}

现在在这里,我需要做的就是这个。

  1. 从com2
  2. 中选择一项
  3. 在从com2中选择项目时,突出显示com1中的项目。突出显示应永久存在(这意味着,当用户再次点击com1时,突出显示不应消失)
  4. EX:你从com2中选择'Food'。那时,com1中显示“One”。所以,突出显示它

    如何完成此操作?请帮忙!

2 个答案:

答案 0 :(得分:2)

有两种方式

  • 更改UIManager

  • 中的键值
  • isSelected

  • 中覆盖DefaultListCellRenderer

例如

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import javax.swing.*;

public class ComboRendererTest {

    public ComboRendererTest() {
        JComboBox comboBox = new JComboBox();
        comboBox.setPrototypeDisplayValue("XXXXXXXXXXXXXXXX");
        comboBox.addItem(new Double(1));
        comboBox.addItem(new Double(2.25));
        comboBox.addItem(new Double(3.5));
        comboBox.setRenderer(new TwoDecimalRenderer(comboBox.getRenderer()));
        comboBox.setEditable(true);

        JComboBox comboBox1 = new JComboBox();
        comboBox1.setPrototypeDisplayValue("XXXXXXXXXXXXXXXX");
        comboBox1.addItem(new Double(1));
        comboBox1.addItem(new Double(2.25));
        comboBox1.addItem(new Double(3.5));
        comboBox1.setRenderer(new TwoDecimalRenderer(comboBox.getRenderer()));
        comboBox1.setEditable(true);

        JFrame frame = new JFrame();
        frame.add(comboBox, BorderLayout.NORTH);
        frame.add(comboBox1, BorderLayout.SOUTH);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ComboRendererTest comboRendererTest = new ComboRendererTest();
            }
        });
    }
}

class TwoDecimalRenderer extends DefaultListCellRenderer {

    private ListCellRenderer defaultRenderer;

    public TwoDecimalRenderer(ListCellRenderer defaultRenderer) {
        this.defaultRenderer = defaultRenderer;
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        Component c = defaultRenderer.getListCellRendererComponent(
                list, value, index, isSelected, cellHasFocus);
        if (c instanceof JLabel) {
            if (isSelected) {
                c.setBackground(Color.blue);
            } else {
                c.setBackground(Color.red);
            }
        } else {
            c.setBackground(Color.red);
            c = super.getListCellRendererComponent(
                    list, value, index, isSelected, cellHasFocus);
        }
        return c;
    }
}

答案 1 :(得分:0)

使用JComboBox.setRenderer设置自定义渲染器(ListCellRenderer aRenderer)