【问题标题】:hiding part of JList string via custom ListCellRendered通过自定义 ListCellRendered 隐藏部分 JList 字符串
【发布时间】:2013-01-18 02:49:44
【问题描述】:

这是我用来添加类型(一般)、(耳语)、(公会)或(全局)消息的“协议语法”

// add the message in list box
ChatJInternalFrame.modelChatList.addElement("(general)" + characterName + ": " + chatMessage);

这里是我设置列表模型和单元格渲染器的地方:

modelChatList = new DefaultListModel<String>();
listForChat = new JList<String>(modelChatList);
listForChat.setFont(new Font("Lucida Console", Font.PLAIN, 14));
listForChat.setCellRenderer(new ColoredChatListRenderer());

这是我的自定义 cellRenderer:

public class ColoredChatListRenderer extends DefaultListCellRenderer {

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);  

        String s = String.valueOf(value);
        String splitPipe[] = s.split("\\)");        

        if(s.length() > 7 && s.substring(0, 8).equals("~SERVER~")){
            setForeground(Color.red);
        } else if (splitPipe[1].length() > 11 && (splitPipe[1].substring(0,12).equals("DEV Proskier") || splitPipe[1].substring(0,11).equals("DEV Sparkle"))){
            setForeground(Color.orange);
        } else {
            if (splitPipe[0].equals("(general")){
                setForeground(Color.black);
            } else if (splitPipe[0].equals("(whisper")){
                setForeground(Color.magenta);
            } else if (splitPipe[0].equals("(guild")){
                setForeground(Color.blue);
            } else if (splitPipe[0].equals("(global")){
                setForeground(Color.pink);
            }
        }
        return(this);
    }
}

现在这很好用,但是我想我不希望聊天中出现类型(一般)、(耳语)等,只是颜色变化。抱歉,如果这是一个非常简单的问题,我的大脑会因为在聊天窗口上工作而受到我用来切换聊天模式的焦点遍历废话。

有什么简单的方法吗??? 就像只是切掉前几个字符的子字符串一样,我可以使模式长度相同......又名(GEN),(GLO),(GUI),(WHI)

****编辑****

感谢您的帮助,但这对我来说是最简单的解决方案。如果这在某些方面不好,请告诉我。

    if (splitPipe[0].equals("(general")){
        setText(splitPipe[1]);
        setForeground(Color.black);
    } else if (splitPipe[0].equals("(whisper")){
        setText(splitPipe[1]);
        setForeground(Color.magenta);
    } else if (splitPipe[0].equals("(guild")){
        setText(splitPipe[1]);
        setForeground(Color.blue);
    } else if (splitPipe[0].equals("(global")){
        setText(splitPipe[1]);
        setForeground(Color.pink);
    }

【问题讨论】:

    标签: java render jlist cellrenderer


    【解决方案1】:

    创建一个包含两条数据的自定义对象,消息类型和消息文本。将对象添加到 ListModel。然后,您的渲染器可以检查类型以用于突出显示目的,并将文本用于显示目的。

    有关此方法的示例,请参阅:Java: Swing JComboBox, is it possible to have hidden data for each item in the list?。该示例使用组合框,但概念相同。

    【讨论】:

    • 所以我也会使用自定义列表模型,如 ... 很酷很有意义,非常感谢。将在它工作时,然后在它工作时勾选你:D
    • 仅供参考 - 只记得覆盖 toString() 返回字符串表示形式。
    • @MouseEvent 恕我直言,覆盖 toString() 是个坏主意。 toString 应该提供有关对象实例的信息。它还会将 OP“锁定”为String 的特定格式。最好让渲染器完成它的工作并决定如何最好地表示视图中的值。这使 OP 有机会提供多个不同的渲染器,用户可以配置这些渲染器,例如...
    • 我会给你支票,因为我确信你的建议有效,我只是想要简单。我已经编辑了我的问题以包含我的解决方案。感谢您的帮助。
    • @JayAvon 虽然我反对你的解决方案,但如果你想添加/删除/更改任何选项,那么它会慢慢变成一个晚上来维护。虽然您可能没有想到,但 camickr 建议的解决方案实际上要简单得多,从长远来看,更灵活,尤其是与某种格式引擎相结合 - 恕我直言
    猜你喜欢
    • 2013-01-02
    • 2016-10-23
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 2014-02-24
    • 1970-01-01
    • 2011-10-22
    相关资源
    最近更新 更多