简体   繁体   中英

How to change the colour of JComboBox's selected Item?

Please have a look at the following code

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

    }
}

Now in here, what I need to do is this.

  1. Select an item from com2
  2. Highlight the item which is in com1, at the moment you select an item from com2. That highlighting should be there permanently (which means, the highlight should not go away when the user clicks on the com1 again)

EX: you select 'Food' from com2. At that time, 'One' is displayed in com1. So, highlight it

How can I make this done? Please help!

there are two ways

  • change keys value in UIManager

  • override isSelected in DefaultListCellRenderer

for example

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM