• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help understanding custom ListCellRenderers

 
Eric Nielsen
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to implement a custom ListCellRenderer; I've referred to several books and web articles, but I can't seem to get a sense of how I should do this.

The goal: each object should render as the item's name, right justified, and a metric score left justified.

e.g.
Event One 100
Event Two 234
Event Three 15
etc

My first attempts were along the lines of the example in Core Java volume two, where they create a JLabel inside the getListCellRendererComponent. I tried creating two JLabels, changed the layout to a BorderLayout and inserted the JLabels. This worked for a single item list, but on multi-item lists each cell contains all the data overwriting itself. The authors of that book don't recommend this method because it's constructing new compoenents on every call.

So I tried changing to a method where the real work is done in the paintComponent method; however I have no clue as to how I'ld generate the intended effect working at the Graphics level, nor how to even figure out the getPerferredSize issues.

How should I tackle this? It doesn't feel like it should be as hard as I'm making it.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's a simple routine

 
Eric Nielsen
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. Thank you, your approach makes much more sense that what I've been staring at.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic