Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing the color of a JList item  RSS feed

 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

is there a good way to change the color of a JList item that isn't selected? I'm throwing my hand at building a client/server chat app and on the client side, I need to highlight the name of the person (which is in a JList) who has sent a message to the client GUI.

Thanks,
Alan
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this thread http://www.coderanch.com/t/335943/GUI/java/Changing-background-color-JList
 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea how that works! Is the getListCellRendererComponent method called everytime something is added to the JList?
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Smith wrote:I have no idea how that works! Is the getListCellRendererComponent method called everytime something is added to the JList?


Ah called everything something is added? From my understanding of the javadocs is yes because the whole list is repainted hence each cell item is re-rendered. So when an item is added or removed, the whole thing get refreshed (repainted).


Javadoc
Description copied from interface: ListCellRenderer
Return a component that has been configured to display the specified value. That component's paint method is then called to "render" the cell. If it is necessary to compute the dimensions of a list because the list cells do not have a fixed size, this method is called to generate a component on which getPreferredSize can be invoked.
 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I have the component now, just need to make the color update which doesn't seem to be working.

 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing the background color has nothing to do the the JList model. It's the "cell renderer" that does the color change.



 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And as for when the cell renderer is "called", it's the same as the paint() method of a component -- it's done whenever the Swing rendering system thinks it needs to be done. So don't try to do something in your renderer based on when you think it's going to be run, and also don't do anything expensive (e.g. database access) if you can avoid it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!