When is a GWT label's layout calculated? - gwt

I want to create a few labels, add them to an AbsolutePanel, and then calculate what positions they should go in. In order to do that calculation, I need to know how wide the browser has made the labels. When I call label.getOffsetWidth(), I get zero.
Is there anything I can do to force a layout calculation, or do I have to use something like scheduleDeferred() to set my positions after the layout has happened?

All widgets have a height and width of zero until the browser finishes rendering them. So you do have to use scheduleDeferred to wait until it happens.

Related

What's the proper way to align a strechable Text and an image to two ends in Unity UI?

I have an UI element that has a Text and an Image as its children. It has a HorizontalLayoutGroup component that enables Control Child Size and Child Force Expand.
I want the Image has a fixed size, and Text has strechable size controlled by the HorizontalLayoutGroup. So when the Image is set to inactive, the Text fills the whole space and when the Image is active, the Text shrinks a little bit in order to give space to the Image. Right now this part works good.
My second goal is to align them to both ends: the Text in the left and the Image on the right with space in between. But changing Child Alignment can't achieve this.
I tried the following solution:
Add LayoutElement both to Image and Text. On Text, enable Flexible Width and set a a value, on Image, enable Min Width and set to a value. Manually adjust the two values until it seems right.
This solution seems to work, but I don't know why. Is anyone familiar with this?What's the recommended way to do it? Thanks!
I worked it out. On the LayoutElement, treat Preferred Width or Preferred Height as Max Width or Max Height. Enable them, set the value the same with min value. One the other objects that you want to stretch, enable Flexible values. Then all worked as we want.

Width limitations with constraints when window resized

I am trying to add a custom view in an nswindow in my osx app.
I need to give a minimum and maximum width values for the custom view which is located in the centre. The view's width should expand until a certain point (maximum width value) but should stop expanding if user continues to expand the window.
Thanks in advance.
You can do all of this with layout constraints.
First, we need to specify where the view should be relative to the window. For the sake of this tutorial, I'm going to assume you want it centered:
Next, we add our constraint for the minimum width:
To make this a minimum instead of an absolute width, click on the constraint and change it to "Greater Than or Equal" in the Attributes Inspector:
Now do the same thing, making another width constraint for the maximum. This time, set it to "Less Than or Equal":
Now the width constraints are set up. But we're not done. We've now set a minimum and a maximum, but the width is still ambiguous—there is no way for the layout constraint system to decide what exact width between 300 and 700 that it should actually be using at any given point. There are two steps to fix this. First, we need to make sure that the view will be entirely within the window and not run off the edges, so create some Greater Than or Equal constraints making sure it stays within its bounds:
(Also, make a trailing constraint which is set up identically).
Finally, we need one last set of constraints; we want some leading and trailing constraints, marked Equal, but with a lower priority:
(Also add a trailing constraint, identically configured)
What does this one do? Well, it tells us that, unless our other constraints (specifically the maximum width, in our case) make it impossible, we'd like the edges of the view to be the standard distance from the window edge. The reason we use 499 as the priority is because that the value of NSLayoutConstraint.Priority.windowSizeStayPut is 500. The documentation has this to say about .windowSizeStayPut:
It's generally not appropriate to make a constraint at exactly this priority. You want to be higher or lower. Constraints with higher priorities can adjust the window’s size. Constraints with lower priorities must be fulfilled using the current window size.
If we set our constraint to higher than 500, the system would restrict us from making the window too wide for these constraints to be valid. That's not what we want, since we want the edge spacing to expand in this case. So since we want to be able to break this constraint by resizing the window, we set it to slightly less than 500—so, 499. This means that the constraint system will try to put the view here, but if it can't do it because we made the window too wide, it will allow this constraint to break, although it'll still try to get as close as it can without breaking the other constraints. So your view will be at its maximum width, and centered in the window.
Voilà!

Pack text into a JFrame?

I'm wondering if there is a way to make text wrap around when it reaches the end of a frame? When using the drawString method, if a line of text exceeds the length of the frame, it seems to just continue on and not wrap around. Does anyone know a way around this?
JTextArea would probably suit. It displays text and you can set setLineWrap(true).
Wrapping text within a frame is a more complicated behavior than can/should be developed as a single method. For example, when you exceed the pre-assigned vertical area, what should happen? And what size of frame is required for layout?
For all these reasons, "display text wrapping in a frame" should be (and is) a component. That component is JTextArea.

Crystal Reports fields won't align

I have several items that I want to line up. The Width and Height of all fields match. Usually I just use the Align, but it's making it worse. My out of line field which is slighly lower, moves way up, way out of line (with align tops)
So I tried using the Size and Position to set it manually. Several objects have a Y of 0.056, but my last one has 0.061. I change the last one to 0.056 and save it. It doesn't appear to be different, and when I look at it again, it's still 0.061. (When I used align, it went up to 0.028!)
I do NOT have Snap to Grid set (my grid size is 0.083). (Nor does turning on Snap to Grid make them align.) The fields have exactly the same formatting. But unless I can magically make them align by hand, they refuse to align. Why?
You've already done a lot of the regular troubleshooting steps. One more ting that I sometimes do is to select all the fields I want to align, then use the arrow keys and move them up into the section above and then back down into their original section. That usually puts them all into the 0 position. If it dosn't work with all of them selected, try each one separately.

Multiple UILabel(s) repositioning according to wordwrap

I have this interface with multiple UILabels.
On view loading i populate white labelled values with some data from a db.
The problem is, some of that fields are potentially too long for the interface, so i'd like to compute the total height of one label once the text is word wrapped and reposition the 2 labels below (shifting the Y coordinate) accordingly to the previous label's height.
All of this should go inside a UIScrollView to let the user scroll those labels vertically.
Any chance i can do this easily with some control i still don't know, or do i have to do it manually?
Thanks
You'll need to use the NSString UIKit Additions to compute the height you need to set on your UILabel, and then adjust the other controls appropriately.
Specifically, I think you want to use sizeWithFont:forWidth:lineBreakMode: to get the rect for your UILabel.
Alternatively, you could use a UIWebView and display the information as HTML. I don't know if it's necessarily less work, but you'll get a layout that automatically adjusts to the size of its contents.