File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Help understanding custom ListCellRenderers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help understanding custom ListCellRenderers" Watch "Help understanding custom ListCellRenderers" New topic
Author

Help understanding custom ListCellRenderers

Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
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

Joined: Jun 09, 2003
Posts: 4632
here's a simple routine

Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
Wow. Thank you, your approach makes much more sense that what I've been staring at.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help understanding custom ListCellRenderers