Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

set background of JList  RSS feed

 
Jeremiah Elliott
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having problems programmaticly setting the background color of a JList. What I want to do is set the background color based on the index of the list. I need to do some database lookups to know what color to set per item.
I have tried ListCellRenderer, but I am running into problems with it. I need my CellRenderer to be able to call non-static methods.
Am I trying to do this the correct way? Also is there a way to just set the background color of one of the items in the JList without setting the background color for everything?
Thanks in advance, if you can't tell by the question I am a swing noob.
 
Greg Brouelette
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the ways that you can use a JList is to put a vector of objects in it. Those objects can be JLabels. The JLabels will display themselves in whatever way that individual JLabel is told to . You can see an example of this in the Swing set demo where each JLabel in the JList has a different image.
You can set the foreground and background color of each JLabel individually (be sure to set them opaque). So maybe a way to do it is to build your own class that extends AbstractListModel and set that as the Model in your JList. The List model handles the manipulation of the data structure of your list (usually just a Vector or ArrayList). You can then add a method to your list model that changes the color of an individual JLabel in your vector. When the JList repaints next time it should show the new color.
Hope it helps
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go. This is what I use

My (Vector)listReturn[x] is what I got back from my database.
 
Jeremiah Elliott
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply.
This is what I have done.
I load one vector with this:

and another vector with just the data. then create the JList with the html vector. when the list is clicked I get the selected index to get the actual data from the other vector. I started on it before you posted that code, so I went ahead and finished it. is there any disadvantage to doing it like this?
I thought it was _WAY_ clever, but I am a noob after all.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I am never against anything that works. But I will say that I personally have noticed a decline in performance when using <HTML> embedded in my Strings like that. But that is me.
In addition, my method only requires 1 Vector, where yours requires 2. And if you remove something from the JList you are going to have to syncronize both Vectors or your selections will be wrong. So seems like you created more overhead for yourself. Not to mention an extra Object, which isn't considerable these days, but I am all about performance.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!