javaswingwidthcodenameoneboxlayout

Codename One - resizing master/detail layout


My Android app has a special layout in master/detail form: the list on the left in landscape mode has to be as large as it is in portrait mode.

I am porting the app with Codename One.

This is what I have in the mainForm code:

if(isTablet() && !isPortrait()) {

        itemListContainer.setWidth(Display.getInstance().getDisplayHeight());
        editingContainer.setWidth(Display.getInstance().getDisplayWidth()-Display.getInstance().getDisplayHeight());
        
        System.out.println("size "+itemListContainer.getWidth()+" "+itemListContainer.getHeight()); 
        //width was 0, now it is 1536 but the actual layout is the same as before, height is 0
//calls to animate() were here but useless
        
        mainForm.add(itemListContainer).add( editingContainer);
      } else {
      
        mainForm.add(itemListContainer);
        
    }
    

I also tried to call the animate() method.

mainForm layout is BoxLayout.x(), that is, horizontal.

The code above has no effect on size or layout. I get the same layout on the simulator, the left container having the size of the label I put inside it.

What is the right way to resize?


Solution

  • Use setPreferredW(int). setWidth will be overriden by the layout but the preferred width will persist through updates/rotation etc.

    Notice that the preferred size methods are deprecated since they are discouraged for most uses. A better way is using layouts which might not be applicable in this case. E.g. table layout supports placing a component with a width percentage constraint which works for most such use cases.

    Deprecation is there just to draw your attention to that, this API won't go away or stop working...