Swing 中未发生列表选择

发布于 2024-10-14 12:08:11 字数 1384 浏览 3 评论 0原文

嗨,我有一个 JLIST 并为其分配了一个 cellRenderer。但我无法选择列表中的元素。实际上它已被选中,但在视觉上我们看不到它已被选中,这意味着我无法看到列表中选择了哪个项目。

我的列表的屏幕截图: 在此处输入图像描述

预期的是 在此处输入图像描述

第二个屏幕截图没有使用 CellRenderer。但是当我添加 CellRenderer 时,我无法在列表中看到所选项目。

当您将 CellRenderer 添加到列表时,这是正常行为吗?

我做错了什么???

编辑:-

这是我的 CellRenderer 类:

public class ContactsRender extends JLabel implements ListCellRenderer {

    private static final long serialVersionUID = 1L;

    ImageIcon img;

    public ContactsRender(){
        setOpaque(true);
        setIconTextGap(12);
        setBackground(Color.WHITE);
        setForeground(Color.black);
    }

    @Override
    public Component getListCellRendererComponent(JList list,
            Object value, int index, boolean isSelected,
            boolean cellHasFocus) {
        if(value != null){
            User user = (User) value;
            String pres = user.getPresence().toLowerCase();
            if(pres.contains("unavailable")){
                img = new ImageIcon("res/offline.jpg");
            } else {
            img = new ImageIcon("res/online.jpg");
            }
            setText(user.getName());
            setIcon(img);

            return this;
        }
        return null;
    }

HI, I've a JLIST and assigned it a cellRenderer. but i was not able select element in list. Actually it is selected but visually we can not see that it is selected means i was not able to see which item is selected in list.

Screen shot of my list:
enter image description here

and what is expected is
enter image description here

The second screen shot is without CellRenderer. But when i add CellRenderer i was not able to see the selected item in list.

is it normal behaviour that when you add CellRenderer to list.

what am i doing wrong ???

EDIT:-

this is my CellRenderer class:

public class ContactsRender extends JLabel implements ListCellRenderer {

    private static final long serialVersionUID = 1L;

    ImageIcon img;

    public ContactsRender(){
        setOpaque(true);
        setIconTextGap(12);
        setBackground(Color.WHITE);
        setForeground(Color.black);
    }

    @Override
    public Component getListCellRendererComponent(JList list,
            Object value, int index, boolean isSelected,
            boolean cellHasFocus) {
        if(value != null){
            User user = (User) value;
            String pres = user.getPresence().toLowerCase();
            if(pres.contains("unavailable")){
                img = new ImageIcon("res/offline.jpg");
            } else {
            img = new ImageIcon("res/online.jpg");
            }
            setText(user.getName());
            setIcon(img);

            return this;
        }
        return null;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤星 2024-10-21 12:08:11

您错误地实现了单元格渲染器。渲染器负责将渲染器背景设置为选择颜色。

阅读 JList API 并点击有关“如何使用列表”的 Swing 教程的链接,您将在其中找到使用 JList 的工作示例。您还将找到有关编写渲染器和示例的部分。

编辑:另外,我刚刚注意到您正在阅读渲染器代码中的图标。你绝对不应该这样做。该图标只能在创建渲染器时读取一次,然后缓存该图标。每次需要重新绘制单元格时,都会调用渲染器,因此继续读取图标效率不高。

You implemented your cell renderer incorrectly. The renderer is responsible for setting the renderer background to the selection color.

Read the JList API and follow the link to the Swing tutorial on "How to Use Lists" where you will find working examples that use a JList. You will also find a section on writing a renderer and an example.

Edit: Also, I just noticed you are reading your icon in the renderer code. You should never do this. The icon should only be read once when the renderer is created and then you cache the Icon. Every time a cell needs to be repainted the renderer is called so it is not efficient to keep reading the icon.

故笙诉离歌 2024-10-21 12:08:11

在单元格渲染器上,您必须实现 isSelected 为 true 的情况。对于您的 ListCellRenderer

Component getListCellRendererComponent(JList<? extends E> list,
                                       E value,
                                       int index,
                                       boolean isSelected,
                                       boolean cellHasFocus)
{
 if (!isSelected) doThis(index);
 else doThatForSelectedItem(index);
}

On your cell renderer, you have to implement the case for isSelected is true. For your ListCellRenderer :

Component getListCellRendererComponent(JList<? extends E> list,
                                       E value,
                                       int index,
                                       boolean isSelected,
                                       boolean cellHasFocus)
{
 if (!isSelected) doThis(index);
 else doThatForSelectedItem(index);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文