2
votes

I had replicate a strange problem in my code below. I've tested in both simulator and device & the result is same. I have 26 buttons in a container (its layout is flowlayout) which itself is in south of BorderLayout (layout of form). But only parts of buttons are seen. What have I done wrong in following code? revalidate does nothing as well.

setLayout(new BorderLayout());

TextArea questionTextArea = new TextArea("1) question ..........");
Container questionContainer = new Container();
questionContainer.add(questionTextArea);

Container questionAnswerContainer = BoxLayout.encloseY(questionContainer);
add(BorderLayout.CENTER, questionAnswerContainer);

Container optionsContainer = new Container(new FlowLayout(Label.CENTER, Label.CENTER));
for (int i = 0; i < 26; i++) {
    Button optionButton = new Button("i");
    optionsContainer.add(optionButton);
}
optionsContainer.revalidate();

Button skipButton = new Button("SKIP");
Container bottomContainer = BoxLayout.encloseY(optionsContainer, skipButton);
bottomContainer.revalidate();
add(BorderLayout.SOUTH, bottomContainer);
//f.revalidate();

only 7 btns are seen here. skipButton is also not there. Why are other buttons not displayed?

enter image description here

1

1 Answers

0
votes

Don't call revalidate too much as besides slowing the app it might break the layout. It should only be invoked when the entire layout is complete.

FlowLayout is generally flaky for these cases and it is the source of the problem. It requests a preferred width/height where the width is too big and the height is too small and then when it is actually placed into the container it isn't given the required amount of space. This is problematic to fix without reflow which slows down performance significantly...

A workaround would be to use a more deterministic layout like grid layout with auto-fit.