You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by Bill van Melle <bi...@gmail.com> on 2011/03/23 01:33:56 UTC
Does ButtonDataRenderer#setFillIcon not work?
I'm trying to make a button that shows an image scaled to whatever size the
button is. Thus, I want the button's internal image view to have fill=true.
It looks like setFillIcon on the renderer is intended for that, but it
doesn't help. I'm doing
((ButtonDataRenderer)button.getDataRenderer()).setFillIcon(true);
in the code that creates the button. Is that not right?
More complete test case:
-- test.bxml --
<Window title="Button Example" maximized="true"
xmlns:bxml="http://pivot.apache.org/bxml"
xmlns:content="org.apache.pivot.wtk.content"
xmlns="org.apache.pivot.wtk">
<BoxPane>
<PushButton bxml:id="button" preferredWidth="50" preferredHeight="50">
<content:ButtonData icon="@test.jpg" />
</PushButton>
</BoxPane>
</Window>
-- in the main program --
BXMLSerializer bxmlSerializer = new BXMLSerializer();
Window window = (Window)bxmlSerializer.readObject(Test.class, "test.bxml");
PushButton button = (PushButton)
bxmlSerializer.getNamespace().get("button");
((ButtonDataRenderer)button.getDataRenderer()).setFillIcon(true);
(As an aside, what I really want to do is have an image be a button, which
I'm guessing I'd have to create my own custom control to do. For a crude
version of that, I tried
setting styles="{borderColor:null,backgroundColor:null}" on the PushButton
above, but the default TerraPushButtonSkin balked at that, unsurprisingly.)
Re: Does ButtonDataRenderer#setFillIcon not work?
Posted by Bill van Melle <bi...@gmail.com>.
And to follow up on your LinkButton suggestion, it works fine, once I
implement a suitable renderer. LinkButtonDataRenderer uses a TablePane
instead of a BoxPane (not obvious why), but has the same issue of not
providing size constraints to the ImageView, so my renderer looks like this:
public class LinkButtonDataRendererWithFill extends LinkButtonDataRenderer {
public LinkButtonDataRendererWithFill() {
imageView.getStyles().put("fill", true);
getColumns().get(0).setWidth(1, true);
getColumns().get(1).setWidth(-1);
getRows().get(0).setHeight(1, true);
}
}
Re: Does ButtonDataRenderer#setFillIcon not work?
Posted by Bill van Melle <bi...@gmail.com>.
Okay, I created https://issues.apache.org/jira/browse/PIVOT-721
On Thu, Mar 24, 2011 at 5:30 AM, Greg Brown <gk...@verizon.net> wrote:
>
> > I note that setFillIcon has additional problems when the ButtonData
> contains text, which this one-liner doesn't fix. Not sure if that's a bug
> in BoxPane, ImageView, or a fundamental shortcoming of the measurement
> system.
>
> Could you provide more detail on this? What kind of problems did you see?
> If you happen to have a small test case that demonstrates the issue, that
> would be great.
Sure. It appears that when you tell an ImageView to scale down to a
particular height, it still reports its width as being the width of the
original image. I'll illustrate without using PushButton, so that we don't
have to worry about implementing the fix described above. The default
button is a BoxPane containing an ImageView and a Label, so mimic it like
this:
<Window xmlns="org.apache.pivot.wtk">
<BoxPane styles="{fill:true, verticalAlignment:'center'}"
preferredHeight="100">
<ImageView image="@test.jpg" styles="{fill:true}" />
<Label text="Label" />
</BoxPane>
</Window>
If you pick test.jpg to be an image significantly larger than 100x100,
you'll see that the space allocated for the image is still the
original width of the image.
Also note that if you pick an image significantly *smaller* than 100x100, it
is not scaled up. Is that a bug, or does there have to be a width
constraint as well to make that happen?
Also in this example, the Label isn't centered. That's not a bug -- it's
because BoxPane ignores the verticalAlignment when fill is true. Need to
move that style to the children, which makes the ButtonDataRenderer fix a
little more than the one-liner I suggested.
All these observations are also true in the other orientation (vertical
BoxPane with a preferred width).
Re: Does ButtonDataRenderer#setFillIcon not work?
Posted by Greg Brown <gk...@verizon.net>.
> So indeed, setFillIcon has no effect as ButtonDataRenderer is currently written. Seems like a bug. If you agree, I can create a ticket and suggest the one-line fix of doing
>
> getStyles().put("fill", true);
>
> in the ButtonDataRenderer constructor.
Sounds good.
>
> I note that setFillIcon has additional problems when the ButtonData contains text, which this one-liner doesn't fix. Not sure if that's a bug in BoxPane, ImageView, or a fundamental shortcoming of the measurement system.
Could you provide more detail on this? What kind of problems did you see? If you happen to have a small test case that demonstrates the issue, that would be great.
Re: Does ButtonDataRenderer#setFillIcon not work?
Posted by Bill van Melle <bi...@gmail.com>.
>
> Could you use a LinkButton with an image as its button data? Link buttons
> don't draw any additional decoration.
>
Okay. It wasn't really clear to me from the tutorials what the intended use
for one type over the other was, but lack of extra decoration is an
interesting distinction in this case. But LinkButtonDataRenderer doesn't
even have a setFillIcon method, so I'd have to do something about that.
> > ((ButtonDataRenderer)button.getDataRenderer()).setFillIcon(true);
>
> FYI - by default, all buttons share a common renderer instance. Unless you
> have previously called setDataRenderer() on this button with a new instance,
> you will actually be modifying the default renderer (probably not what you
> want).
Perhaps not (though I'm not sure why I wouldn't always want my images to
size to the button they're in). But that doesn't tell me why setFillIcon
didn't do what I wanted. I thought about it a little more and realized that
the ImageView must not be getting a size constraint. Sure
enough, ButtonDataRenderer is our old friend BoxPane, and it does not have
its fill style set to true. Thus, the following workaround does the trick:
public class ButtonDataRendererWithFill extends ButtonDataRenderer {
public ButtonDataRendererWithFill() {
imageView.getStyles().put("fill", true);
this.getStyles().put("fill", true);
}
}
and then in my user code
button.setDataRenderer(new ButtonDataRendererWithFill());
So indeed, setFillIcon has no effect as ButtonDataRenderer is currently
written. Seems like a bug. If you agree, I can create a ticket and suggest
the one-line fix of doing
getStyles().put("fill", true);
in the ButtonDataRenderer constructor.
I note that setFillIcon has additional problems when the ButtonData contains
text, which this one-liner doesn't fix. Not sure if that's a bug in
BoxPane, ImageView, or a fundamental shortcoming of the measurement system.
Re: Does ButtonDataRenderer#setFillIcon not work?
Posted by Greg Brown <gk...@verizon.net>.
> what I really want to do is have an image be a button,
Could you use a LinkButton with an image as its button data? Link buttons don't draw any additional decoration.
> ((ButtonDataRenderer)button.getDataRenderer()).setFillIcon(true);
FYI - by default, all buttons share a common renderer instance. Unless you have previously called setDataRenderer() on this button with a new instance, you will actually be modifying the default renderer (probably not what you want).
G