删除JComboBox中的突出显示 [英] Delete highlighting in JComboBox

查看:90
本文介绍了删除JComboBox中的突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当刚创建并添加了JComboBox并且所选项目的所有背景都是正常的白色时:
(忽略文本后的巨大间距)

When a JComboBox is just made and added and all the background of the selected item is just normal and white:
(ignore the enormous spacing after the text)

然后我打开列表并将光标悬停在某个项目上时,该项目将突出显示,这一切正常,没有任何问题.

When I then open the list and hover my cursor over an item, that item get's highlighted, all normal, nothing wrong with it.

但是现在的问题是,当我单击某个项目时,突出显示仍然存在:

But the problem now is that the highlighting stays once I've clicked on an item:

所以我的问题是:
如何使突出显示消失?
最好不要处理来自社区的程序包或过载或其他任何问题.

如果我是对的,那么它必须位于组合框的动作侦听器的根目录"中吗?
所以:

So my question is:
How can I make the highlighting go away?
Preferably without doing difficult with packages from the community or overloading or whatever.

If I'm right it has to be in the 'root' of the action listener of the combo box?
So:

public void actionPerformed(ActionEvent e)
{
    if(e.getSource() == comboBox)
    {
        // code to delete the highlighting
    }
}

推荐答案

为了使遇到类似问题的人更轻松,这是我编写的渲染器的代码:

To make it easier for people with a similar problem, here is the code for the renderer I wrote:

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

class ComboBoxRenderer extends JLabel implements ListCellRenderer
{
    private boolean colorSet;
    private Color selectionBackgroundColor;

    public ComboBoxRenderer()
    {
        setOpaque(true);
        setHorizontalAlignment(LEFT);
        setVerticalAlignment(CENTER);
        colorSet = false;
        selectionBackgroundColor = Color.red; // Have to set a color, else a compiler error will occur
    }

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
    {
        // Check if color is set (only runs the first time)
        if(!colorSet)
        {
            // Set the list' background color to original selection background of the list
            selectionBackgroundColor = list.getSelectionBackground();
            // Do this only one time since the color will change later
            colorSet = true;
        }

        // Set the list' background color to white (white will show once selection is made)
        list.setSelectionBackground(Color.white);

        // Check which item is selected
        if(isSelected)
        {
            // Set background color of the item your cursor is hovering over to the original background color
            setBackground(selectionBackgroundColor);
        }
        else
        {
            // Set background color of all other items to white
            setBackground(Color.white);
        }

        // Do nothing about the text and font to be displayed
        setText((String)value);
        setFont(list.getFont());

        return this;
    }
}

以前的代码似乎无法正常工作,代码已更新,现在应该可以正常工作

Previous code didn't seem to work as properly, code updated and should work all fine now

这篇关于删除JComboBox中的突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆