There's not a whole lot of help, even in the Javadoc, for knowing what
a Renderer should look like.  In my case, I wanted to implement an
ItemRenderer for ListView. After struggling for quite a while trying
various approaches, which either displayed nothing or outright broke
in the renderer's skin's paint method with null references, I looked
again at the Pivot sources and found the subtle element that, I'm
guessing, pretty much every Renderer needs to do if it's based on a
Component:

    @Override
    public void setSize(int width, int height) {
        super.setSize(width, height);

        // Since this component doesn't have a parent, it won't be validated
        // via layout; ensure that it is valid here
        validate();
    }

I'm wondering if this is the problem that ccp999 was having
(http://apache-pivot-users.399431.n3.nabble.com/Using-BXML-for-ListView-ItemRender-td3345692.html).

However, before coming to this discovery, I'd already written and was
about to send a much longer message about my problems (funny how
writing about problems sometimes solves them), which included my
understanding of what a Renderer is supposed to do, which I include
below.  Assuming I've got it right, I'd love to have something like
this show up in the documentation to save others from a long discovery
process.  It should, of course, include the essential tidbit above
about calling validate.


An item renderer for ListView is a Component* that implements a couple
extra methods, of which "render" is the interesting one -- it gets
called when the ListView wants to layout its items or paint them.  The
renderer's job is to modify itself to look exactly like what the
ListView should paint in the space allocated for the item that was
passed to render.  There are some extra arguments to render to allow
you to customize your look according to whether the item is selected,
highlighted etc.  The ListView all by itself is setting the background
of each item according to that selection/highlight/whatever state, so
the extra arguments are useful if you want to be harmonious with it,
e.g., by making your text's foreground color be white (actually, the
ListView's selectionColor style) when the item is selected.  That last
bit was really non-obvious to me for some while.

(*Strictly speaking, it doesn't have to be a Component, merely
something that implements ConstrainedVisual, but Component does
implement ConstrainedVisual, and I expect nearly everyone would use
some sort of Component to make the implementation tractable.)

It looks like other renderers are similar, differing mainly in the
nature of the extra arguments to render, which depend on the
capabilities of the container.

Reply via email to