Skip to main content

When & how to get component actual size

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
9 replies [Last post]
tempusername
Offline
Joined: 2009-03-25
Points: 0

when inheriting from Component to make your own component and you want it to be working for all sizes (i.e. like a fill-parent style component or streatchable)

when & how is the correct time to query the component width & height?

the when & how are both tightly related because i can assume that the when should be after the component was layout the its container but i dont know which method is called when that is done so i can override it and check for the size (which also btw which method to call to query the size since there are several)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vprise
Offline
Joined: 2003-11-07
Points: 0

Notice that LWUIT works for all sizes with its own components and the solution is NOT knowing the size.

When you need the size query the component for it but only at the last moment (e.g. when asked to paint yourself). At all other times the actual size is inconsiquential and wouldn't help you. It would also probably be wrong.

If you can be specific about what you are trying to acheive I can point you more accurately in the right direction.

tempusername
Offline
Joined: 2009-03-25
Points: 0

Hey Shai,

Well ofcourse currently im doing it in the paint method but i think it might not be the best solution for all situations.

i started to make a component that needs to display an image to cover its entire dimension and that image needs to be downloaded from the web based on the dimension
(resizing an image and getting a fixed dimension image is out of the question due to quality reasons)
i know i can do this initialization and retrieving from the net async but still that means that i have to load the current form with that component and only when the component's first paint method is called i can begin this process and not sooner.

vprise
Offline
Joined: 2003-11-07
Points: 0

Usually when faced with situations like these I download the image based on the screen size not the eventual component dimension.

Think about it this way: component size is calculated based on layout and preferred size. What size are you asking for in calcPreferredSize()?

If you are using some layouts such as flow layout, box layout (partially) etc. you will be getting something that has close bearings to your request. However, because the size of your component depends on the size of all other components (and scrollability and the screen size and font size etc...) we aren't sure about it until the last minute (otherwise we might calculate things we don't really need to know or things that might change (e.g. by adding more components before showing the form).

tempusername
Offline
Joined: 2009-03-25
Points: 0

I'm not asking for any size in calcPreferredSize nor via setPreferredSize

i want the component to fill the parent and then fill in the image based on its size

vprise
Offline
Joined: 2003-11-07
Points: 0

When creating a custom component you MUST override calcPreferredSize() otherwise how would LWUIT know the size the component needs for the layout manager.

If you want the component to be big at first calcPreferredSize() should return the size of the screen (it won't get it but it will take as much as possible and maybe scroll depending on the screen). Then when you get the image data and know the exact size just invoke setShouldCalcPreferredSize(true) and now the calcPreferredSize() method should return the size of the image which will make sure the layout manager gives it approximately that size.

tempusername
Offline
Joined: 2009-03-25
Points: 0

what do you mean i MUST override calcPreferredSize in my component i dont override it...
calcPreferredSize isnt abstract infact even in the javadocs it doesnt say to override it when inheriting from component.

if i override calcPreferredSize and set it to full screen wont the component get the full screen size if that form has scrolling? (if it doesnt i think it wont get full screen correct?)

vprise
Offline
Joined: 2003-11-07
Points: 0

I should have qualified it as: You must override it when you want a specific size for a component (which you obviously do). This is stressed in the developer guide but we probably should update the javadocs with better guidelines for custom component creation.

calcPreferredSize() works in conjunction with layout calculations to determine the optimial screen size for the layout, ultimately the layout decides the size of the final component and it does that while considering flags such as scrollability. Usually when we want a component to take up the entire screen we just place it in the center of the border layout, that particular layout ignors the preferred size of the component in the center and always stretches it to available space. The logic is explained but we find people avoid the elaborate layout/prefSize explanations and rush to actual samples (such as the layout demo).

tempusername
Offline
Joined: 2009-03-25
Points: 0

Actually no, i dont want to specific a size for a component (like i wrote in the previous messages) i want to KNOW the size of the component after it will be layed out but preferably before the first paint method is called so i can perform some initialization based on its size.

Since layout occurs only when the form is shown (because like you said up untill that point components can still be added - though now that im thinking about it components can be added also after the form was shown)
maybe it would be done by calling layoutContainer of the form but the problem is that setShouldLayout is protected so the layoutContainer can only be called once but i guess thats ok for my case...

vprise
Offline
Joined: 2003-11-07
Points: 0

Indicating the preferredSize is something you do regardless, it doesn't mean that the size you get will match the preferred size. You need to do this otherwise your component will have 0 width/height in many layouts.

Since the screen orientation can change and a VKB might popup the component size will always be temporary and can change while the component is showing. Furthermore components added to a form in runtime might affect the basic size of the component so your assumption that there is a fixed component size is problematic. What we do for cases like yours is simple:

When painting the data we check if the component is initialized if not we use a placeholder value (like the image browsers/apps show while loading the actual image)

If the component is initialized that means the size is determined at least for now, in that case we check if the width/height of the data we fetched matches the current component width/height, if not we request new data asynchroniously. You can use a service in LWUIT4IO such as the ImageDownloadService which will fetch and replace an image directly into your component.