jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes ListCellRenderer!!!Help!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ListCellRenderer!!!Help!!" Watch "ListCellRenderer!!!Help!!" New topic
Author

ListCellRenderer!!!Help!!

gautham kasinath
Ranch Hand

Joined: Dec 01, 2000
Posts: 583
Hi!
Well I would like to know how to use the CellRenderer in JList to show different colors to different entries in a List.
Tanx
Regds
Gautham Kasinath


"In the country of the blind, the one eyed man is the King"
Gautham Kasinath CV at : http://www.geocities.com/gkasinath
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521

Per the 1.3 API -
// Display an icon and a string for each object in the list.

class MyCellRenderer extends JLabel implements ListCellRenderer {
final static ImageIcon longIcon = new ImageIcon("long.gif");
final static ImageIcon shortIcon = new ImageIcon("short.gif");
// This is the only method defined by ListCellRenderer.
// We just reconfigure the JLabel each time we're called.
public Component getListCellRendererComponent(
JList list,
Object value, // value to display
int index, // cell index
boolean isSelected, // is the cell selected
boolean cellHasFocus) // the list and the cell have the focus
{
String s = value.toString();
setText(s);
setIcon((s.length() > 10) ? longIcon : shortIcon);
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}
else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
setFont(list.getFont());
return this;
}
}
String[] data = {"one", "two", "three", "four"};
JList dataList = new JList(data);
dataList.setCellRenderer(new MyCellRenderer());

JList doesn't provide any special support for handling double or triple (or N) mouse clicks however it's easy to handle them using a MouseListener. Use the JList method locationToIndex() to determine what cell was clicked. For example:
final JList list = new JList(dataModel);
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int index = list.locationToIndex(e.getPoint());
System.out.println("Double clicked on Item " + index);
}
}
};
list.addMouseListener(mouseListener);

Note that in this example the dataList is final because it's referred to by the anonymous MouseListener class.
For the keyboard keys used by this component in the standard look and feel (L&F) renditions, see the JList key assignments.

"JavaRanch, where the deer and the Certified play" - David O'Meara
gautham kasinath
Ranch Hand

Joined: Dec 01, 2000
Posts: 583
Hi!
Well what I am trying to do is specifically this.
I have a list of users in a chat GUI displayed in awt.list right now for the time being.
My program gives users an option to ignore certain users 1 or more.
now I am trying to display the ignored user names displayed in the list field in color = red and those not ignored in color = green.
To do this my logical conclusion was to look up swing and that I did and further looked up the code given in the java api for cell renderer.
well it is the same as the one u sent me.
but then my requirement is still not fullfilled...
please help me on the same
Thanx
Regds
Gautham Kasinath
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
hi gautham,
here is a long code . just run it first and see if this is what you want. and if yes then try and understand it's long but simple.


regards
Deekasha

[This message has been edited by deekasha gunwant (edited December 09, 2000).]
[This message has been edited by deekasha gunwant (edited December 09, 2000).]
[This message has been edited by deekasha gunwant (edited December 09, 2000).]
[This message has been edited by deekasha gunwant (edited December 09, 2000).]
[This message has been edited by deekasha gunwant (edited December 09, 2000).]
gautham kasinath
Ranch Hand

Joined: Dec 01, 2000
Posts: 583
hi! Deeksha
Well i tried running u r code and it did run!!
but alas it opens in an Appletviewer but wen I modified it to run on IE it didnot..
ne way lemme look around the problem.
Meanwile I got a strange happenning on my code..
well I have the code posted here :

Well here the error that I am getting is that when I select a color and type a message the message is displayed in the color I chose but then when I change the color.. even before typing anything the color of all the text in the JTextPane changes to black.
I dunno y..
well I ve tried to figureout why this is happenning for a little while now but to no avail.
Please review the code and temme if something is wrong.
I would like you reply to : gkasinath@hotmail.com
thanks
Regds
Gautham Kasinath
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ListCellRenderer!!!Help!!