• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Help understanding custom ListCellRenderers  RSS feed

 
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.
 
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.
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!