• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

ListCellRenderer!!!Help!!

 
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 583
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Are we home yet? Wait, did we forget the tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic