Question

Here is the 'protocol syntax' I am using to add messages of type (general), (whisper), (guild), or (global)

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

Here is where I setup my list's model and cell renderer:

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

And here is my custom 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);
    }
}

chat modes

Right now this works perfectly, however I think I don't want the type (general), (whisper), etc showing up in the chat, just the color change. Sorry if this is a real easy question, my brain hurts from working on the chat window with focus traversal crap I used to switch chat modes.

Is there some easy way to do this??? like just substring chopping off the first few chars, I can make the modes same length... aka (GEN), (GLO), (GUI), (WHI)

****EDIT****

I appreciate the help but this was the simplest solution for me. Please let me know if this is bad in some way.

    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);
    }
Was it helpful?

Solution

Create a custom Object that contains two pieces of data, the type of message and the text of the message. Add the object to the ListModel. Then your renderer can check the type for highlighting purposes and use the text for display purposes.

See: Java: Swing JComboBox, is it possible to have hidden data for each item in the list? for an example of this approach. The example uses a combobox but the concept is the same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top