Container properties

41 views
Skip to first unread message

jazo...@gmail.com

unread,
Aug 12, 2013, 12:19:28 PM8/12/13
to codenameone...@googlegroups.com
Hi, I´m trying to get coordinates(absoluteX and absoluteY), width and height values of a container from my "theme.res" file.

So im using the "protected void beforeMain(Form f)" as an event method. Inside that method im using findContainer() to get the container object, but all values are 0.

So I guessed that´s because on beforeMain the layout managers havent worked yet... so I tried to do the same on "postMain(Form f)", but findContainer() get a Null in that method.

What I´m missing?

Shai Almog

unread,
Aug 12, 2013, 12:47:36 PM8/12/13
to codenameone...@googlegroups.com, jazo...@gmail.com
First you must always use the find version that accepts the argument.
Second the concept is wrong.

You could invoke layoutContainer on the form and then just get the actual position before it is shown but that is never (under no use case I ever came across) the right thing to do statically. A device will get rotated, a VKB will rise changing screen dimension not to mention other potential issue.

If you will tell us why you want to use getAbsoluteX/Y() I think I can give you a much better answer.

jazo...@gmail.com

unread,
Aug 12, 2013, 1:31:59 PM8/12/13
to codenameone...@googlegroups.com, jazo...@gmail.com
Thanks for the tip on using only the version that accepts the argument.

I need to draw "lines" and "images" that are perfectly aligned on top of current containers.
See my spec (drawed on photoshop):


And this is my current app (on codename one):

So what im attempting to do is add a layer on top and override the container's paint method and paint the connecting lines.
For the white horizontal lines, I will set a border on every "level" container (level = group of 4 color squares).

If you have a better way please help me.

Thanks in advance.

Shai Almog

unread,
Aug 13, 2013, 1:19:32 AM8/13/13
to codenameone...@googlegroups.com, jazo...@gmail.com
Cool looking app!
Be sure to submit it to the gallery when you are done.

Need to do these types of calculations dynamically in the paint method so the lines will be drawn to the right location.

The easiest way to do this is to define your own BGPainter and set it to the Container, this doesn't require subclassing and the paint method will be invoked every time the container is drawn (it will however draw behind the content).

You could also create your own container and override its paint method (or paintGlass method) and just draw there, in this case you would call super to perform the actual drawing of your elements but then draw your lines afterwards. At that point all components would already be sized, positioned and getAbsoluteX/Y would represent a real value. You would also need to take scrolling into account at that point.

I'm assuming the reason you didn't do it that way is that its hard to derive in the GUI builder. You could do it though by just replacing the existing container with your derived instance (although that is a bit awkward).

Notice that for both cases I mentioned above, getAbsolute* will return a position in screen coordinates and you need a position relative to the container to draw so you will need to subtract your containers absolute position too.
Reply all
Reply to author
Forward
0 new messages