Jiasheng Qin
Jiasheng Qin

Reputation: 55

Setting widthUnit/heightUnit for a Sizeable object in Vaadin

For a sizeable object (https://vaadin.com/api/framework/7.7.3/com/vaadin/server/Sizeable.html), we obtain the width and height using getWidth() and getHeight() respectively, and the unit via getWidthUnit() and getHeightUnit(). How do I set the width/height unit?

EDIT: So, the reason I ask is because I have a Panel and I'm working on a function that automatically scrolls it to a certain line. The Scrollable interface only provides setScrollTop in terms of pixels, so I would want to do something like:

panel.setScrollTop(lineNumber/totalLines * heightOfPanelInPixels)

For that, I'm planning on using the SizeReporter addon to give me "heightOfPanelInPixels", but I'm not sure I can guarantee the height being in terms of pixels. If it's not, I would need some way to convert the units.

Also, I asked about setting the unit since I thought it was just a universal thing: like if I could just set the unit for all Sizeables to report in, say, inches or pixels

Upvotes: 0

Views: 125

Answers (1)

Leif Åstrand
Leif Åstrand

Reputation: 8001

There isn't really any way of achieving what you want to do, short of setting all sizes as px values from the server. For any other kind of size definition, the actual size in pixels will vary depending on lots of different factors that only the browser keeps track of. Furthermore, there isn't any mechanism that would pass back the actual sizes to the server as they are resolved.

You might want try this add-on that makes the size of selected components available on the server: https://vaadin.com/directory/component/sizereporter.

Old answer below:

There are two ways of setting the size in either direction. They both lead to exactly the same end result - it's just two different ways of expressing the same intent.

Taking the height as an example, there's the setHeight(String) method that expects a CSS definition such as 20px or 3.5em. This method is convenient to use of you want to set a hardcoded size directly from code.

The other approach splits up the size into a numerical size and a separate unit: setHeight(float, Unit), e.g. setHeight(20, Unit.PX). This method is more practical if you want to do calculations with the size, e.g. doubling it by using setHeight(2 * getHeight(), getHeightUnit()).

Setting and getting widths also work in exactly the same way.

Upvotes: 2

Related Questions