Split screen proportionally LWUIT

330 views Asked by At

I want to split the screen by 30% and 70% vertically, How can i achieve this with lwuit? I used/tried GridLayout but it splits the screen equally. Need a example code for this.

Thanks in advance!

2

There are 2 answers

0
Shai Almog On BEST ANSWER

Both other answers will fail when rotating the screen of a device.

You can take two approaches, use a table layout which supports percentage distribution of layout constraints.

Or create a subclass of Contaienr that overrides the calcPreferredSize method and returns a dimension of 30 or 70 percent appropriately. Then just add both of them to a BoxLayout container and use as desired e.g.:

Container c30 = new Container() {
      public Dimension calcPreferredSize() {
          new Dimension(Display.getInstance().getPreferredHeight(), (int)(Display.getInstance().getPreferredWidth() * 0.7));
      }
};
0
pheromix On

Create a class which derives Container :

public class split extends Container {
    public split(int h)
    {
        super();  // you can set your layout type here
        setPreferredH(h);
    }
}

Then add components of this class in your Form :

public class e extends Form {
    private Container c1, c2;
    private TextField f1,f2;
    public e()
    {
        super("test split");
        c1 = new split(30*getPreferredH()/100);
        c2 = new split(70*getPreferredH()/100);
        f1 = new TextField("ghgjhg");
        f2 = new TextField("jkdhuhg");
        c1.addComponent(f1);
        c2.addComponent(f2);
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        addComponent(c1);
        addComponent(c2);
    }
}

You can even set a backgroundPainter to the split class to show visually the splitting.