Fork us on GitHub

Same Size & Back Swipe

Codename One inherited basic layout concepts from Swing which...

Same Size & Back Swipe

Codename One inherited basic layout concepts from Swing which in turn inherited them from AWT. We modernized and adapted them quite a bit by removing various behaviors and adding others, but a key to sizing and placing components is the preferred size.
Every component calculates its own preferred size in the calcPreferredSize() method. This calculation takes into account the font, icon, padding and theme to produce the space required by the given component but sometimes that's not good enough.

Preferred size is just one of the values the layout managers take into account when placing components; so you can use layout managers in pretty much any way you want to achieve the desired placement. However, there was one use case that was remarkably elusive for us with this approach: sizing a group of components to have the same size.

Normally, we would do this by finding the largest preferred size in the group then invoke setPreferredSize on all the various components. This is hugely problematic since its both a hassle and changes to the theme, text etc. of a component in the group wouldn't be reflected. Now we have a solution: Component.setSameWidth(Component... c); & setSameHeight(Component... c)

So we can effectively do something like Component.setSameWidth(cmp1, cmp2, cmp3, cmp4); and they would all have the same preferred width that will be maintained even if we change the text of one of the components. Since that is the biggest use case for setPreferredSize()/W()/H() we are effectively deprecating these methods. Its generally bad practice to use them and would restrict the portability of your code so we highly recommend you use an alternative means for sizing components.

One of our enterprise developers pointed out functionality in iOS7 that allows swiping back a form to the previous form. This is pretty hard to accomplish in Codename One because we expect transitions to be non-interactive. However, we decided to pick up that challenge since we want to upgrade transitions for iOS/Android to more closely match the new designs (e.g. Material) so we added a new API to enable back swipe transition: SwipeBackSupport.bindBack(Form currentForm, LazyValue<Form> destination);

That one command will enable swiping back from currentForm. LazyValue is a new interface that we will probably use quite often moving forward, its defined as:
public interface LazyValue<T> {
    public T get(Object... args);

Which effectively allows us to pass a form and only create it as necessary (e.g. for a GUI builder app we don't have the actual previous form instance), notice that the arguments aren't used for this case but will be used in other cases (and are used in some new capabilities in the motion).

Notice: This post was automatically converted using a script from an older blogging system. Some elements might not have come out as intended.... If that is the case please let us know via the comments section below.

Share this Post:

Posted by Shai Almog

Shai is the co-founder of Codename One. He's been a professional programmer for over 25 years. During that time he has worked with dozens of companies including Sun Microsystems.
For more follow Shai on Twitter & github.