0

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);
    }
KisnardOnline
  • 653
  • 4
  • 16
  • 42

1 Answers1

2

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.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
  • so i would also use a custom list model of like , ... cool makes sense, thanks a bunch. Will work on it now and then check mark you when it works :D – KisnardOnline Feb 03 '13 at 03:51
  • FYI - just remember to overwrite `toString()` returning the string representation. – Mordechai Feb 03 '13 at 04:11
  • @MouseEvent IMHO, overriding `toString()` is a bad idea. `toString` should provide informative information about the instance of the object. It would also "lock" the OP into a particular format of the `String`. Better to allow the renderer to do it's job and decide how best to represent the value within the view. This allows the OP the opportunity to supply multiple different renderers, which the user might be able to configure, for example... – MadProgrammer Feb 03 '13 at 04:33
  • I am going to give you the check because I am sure your suggestion works, I just wanted simplicity. I have edited my question to include my solution. Thanks for the help none the less. – KisnardOnline Feb 03 '13 at 04:47
  • @JayAvon while I have thing against your solution, if you wanted to add/remove/change any options, then it's honing to slowly become a night are to maintain. While you may no think it, the solution suggested by camickr is actually far simpler and in the long term, more flexible, especially coupled with a format engine of some kind - IMHO – MadProgrammer Feb 03 '13 at 05:04