Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2004

Tip: Looking for answers? Try searching our database.

Controlling layout of text in ListCellRenderer

Thread view: 
Jim Cobban - 02 Nov 2004 00:51 GMT
I am trying to write a ListCellRenderer that gives me more explicit control
of the layout of the text than does extending JLabel.  I am not getting very
far.

In this particular case there are three columns of text to display in each
cell:

A single numeric digit row number
A three character spreadsheet column identifier, which is to be displayed
right-aligned in a 3-M wide space
A description of the contents of the column, which in all cells is to be
displayed at the same position (5 Ms in) in all cells of the list.

The only way I have been able to get this to work so far is by enclosing the
three bits of text inside appropriate HTML.  However, as is warned about in
the description of this capability, this is very slow.  So I would like to
draw the text myself in the ListCellRenderer.  However I find the
description in the documentation cryptic and everything that I try does not
work.  Figuring that java.awt.Label would give me some hints on how to paint
the text I browsed through its source, but it doesn't do the paint, and I
cannot for the life of me see how it does its job without that paint.  It
just puts the text value into a String object and then NEVER looks at it
that I can see.

One concern is that I cannot figure out how to let JList know how wide to
display the cells.  The minimum width of the Component I return from my
ListCellRenderer has to be wide enough to hold all of the text I am going to
write.  But according to the documentation, and experimentation, it is not
possible to get the instance of FontMetrics for the current Font until the
Component is already displayed!  Component.getFontMetrics blows up with a
NullPointerException because the hashtable of instances of FontMetrics has
not been initialized.  If I cannot get the FontMetrics instance then I
cannot calculate the minimum width of the cell.

I genuinely feel that the code for AWT and SWING is unnecessarily obscure in
its implementation.
Andrei Kouznetsov - 02 Nov 2004 11:17 GMT
> One concern is that I cannot figure out how to let JList know how wide to
> display the cells.  The minimum width of the Component I return from my
[quoted text clipped - 5 lines]
> not been initialized.  If I cannot get the FontMetrics instance then I
> cannot calculate the minimum width of the cell.

take FontMetrics from JList.

Signature

Andrei Kouznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Jim Cobban - 02 Nov 2004 19:24 GMT
Thanks.  That does the trick.  It is nice and fast now that I am using a
class derived directly from the base java.awt.Component without all of the
overhead of JLabel etc.  Of course as I go along I will find all of the
gotchas, things I have to explicitly take care of that Swing does for me.

> > One concern is that I cannot figure out how to let JList know how wide to
> > display the cells.  The minimum width of the Component I return from my
[quoted text clipped - 8 lines]
>
> take FontMetrics from JList.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.