java - 永久更改 JComboBox 所选项目的颜色

标签 java swing user-interface jcombobox

请看下面的代码

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

public class JCombo extends JFrame
{
    JComboBox com1;

    public JCombo()
    {


        com1 = new JComboBox();

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

        com1.addItemListener(new Com1Action());

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

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

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

    }
}

在这里,当一个项目被选中时,我想将颜色应用到所选项目。我怎样才能做到这一点?

例如:

User selects "One" - Now "One" changes to blue
  User selects "Two" - Now "Two" changes to blue. "One" is also blue as well, because we changed the colour at the first place
    User selected "Three" - Now "Three" changes to blue. "One" and "Two" remains blue as well

更新

我用自定义渲染器重新编码了它。现在它是突出显示所选项目,但只要鼠标移动,颜色就会恢复到原来的状态。换句话说,这里唯一发生的事情是更改突出显示颜色,而不是永久

将颜色应用于所选项目
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class JCombo extends JFrame
{
    JComboBox com1;

    public JCombo()
    {


        com1 = new JComboBox();
        com1.setLightWeightPopupEnabled(true);

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

        com1.setRenderer(new MyCellRenderer());

        com1.addItemListener(new Com1Action());

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

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

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

            }
        }
    }

   class MyCellRenderer extends JLabel implements ListCellRenderer<Object> 
   {
     public MyCellRenderer() 
     {
         setOpaque(true);
     }

     public Component getListCellRendererComponent(JList<?> list,
                                                   Object value,
                                                   int index,
                                                   boolean isSelected,
                                                   boolean cellHasFocus) {

         setText(value.toString());

         Color background = Color.white;
         Color foreground = Color.black;

         // check if this cell represents the current DnD drop location
         JList.DropLocation dropLocation = list.getDropLocation();

         if (dropLocation != null
                 && !dropLocation.isInsert()
                 && dropLocation.getIndex() == index) {



         // check if this cell is selected
         } else if (isSelected) {
             background = Color.RED;
             foreground = Color.WHITE;

         // unselected, and not the DnD drop location
         } else {
         };

         setBackground(background);
         setForeground(foreground);

         return this;
     }
 }


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

    }
}

最佳答案

这需要 Providing a Custom Renderer ; API 包含一个 example .

附录:唯一发生的事情是更改突出显示颜色,而不是永久将颜色应用于所选项目。

永久更改某些内容意味着有一个地方可以存储该信息。我看到两个选择:

  • state 字段添加到您选择的 ComboBoxModel 并使用它来调节渲染器中的背景颜色。您可以使用 list.getModel() 访问渲染器内的模型。

  • 切换到JListJTable 并使用允许多项选择的ListSelectionModel,如建议的那样here作者:@mKorbel。

关于java - 永久更改 JComboBox 所选项目的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13671666/

相关文章:

java - 如何限制 Google Maps API AutocompleteService 提供越界建议

java - 打开 HTTP 连接后如何识别 content-type?

java - 在 Controller 测试中注入(inject)主体

java - 使用 JTextField 和 BoxLayout 设置 Swing 格式

java - 来自 AWT JFrame 的真正应用程序模态 SWT shell

user-interface - 在 MonoDevelop 中打开 GUI 设计器

java - 如何在 Android 的后台服务中使用相机拍照?

java - 如何将复选框 [组合框] 列表添加到 Java Netbeans Swing 应用程序中的 JScrollPane

java - 禁用自动调整大小,而不禁用手动调整大小

SQL Server GUI 在访问大量行和数据时保持卡住