• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2 colours for items in JLIST

 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it possible to have 2 different colours for items
in a jlist lets say completed items would be shown in
black and outstanding in red
Thank u
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to write your own ListCellRenderer and a data class. That is for instance extend JLabel and implement the appropriate interface. Please take a look at the api documentation of the ListCellRenderer interface. The only to implement method is
<code>public Component getListCellRendererComponent(...)</code>
So you have to define your data class, for instance:
<code>public class MyData
{
private String mDescription = "default";
public String getDescription()
{
return mDescription;
}
public void setDescription(String description)
{
mDescription = description != null ? description : "default";
}
}</code>
and define the cell renderer:
<code>class MyDataCellRenderer extends JLabel implements ListCellRenderer
{
public MyDataCellRenderer()
{
setOpaque(true);
}

public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
if (value instanceof MyData)
{
MyData myData = (MyData) value;
setText(myData.getDescription());
if (myData.isCompleted())
{
if (isSelected)
{
// change the colors to the desired:
setBackground(list.getSelectionBackground());
setForeground(Color.red);
}
else
{
// change the colors to the desired:
setBackground(list.getBackground());
setForeground(Color.red);
}
}
else
{
if (isSelected)
{
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}
else
{
setBackground(list.getBackground());
setForeground(list.getForeground());
}
}
}
return this;
}
}</code>
Finally you have to make your ListCellRenderer the cell renderer of the JList: <code>myJList.setCellRenderer(...);</code>
and you add elements of MyData to the JList...
You can find another example in the api documentation of JList...
Good luck
Tom

[This message has been edited by Thomas Suer (edited October 17, 2001).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic