I have been trying to implement a partially Dynamically generated GUI but an having trouble with the layout not rendering properly. Simply resizing the window corrects the issue. Here is the problimatic generation code:

public void setupBookTabs() {
    ArrayList<String> bookTabNames = Record.getBookGroups();
    for (int i = 0; i < bookTabNames.size(); i++) {
    JPanel jPanel = new JPanel();
        jPanel.setName(bookTabNames.get(i));
        jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.PAGE_AXIS));
        ArrayList<Book> bookGroup = Record.getBooksByGroup(bookTabNames.get(i));
        for (int k = 0; k < bookGroup.size(); k++) {
            JPanel innerPanel = new JPanel();
            innerPanel.setName(bookGroup.get(k).getName());
            innerPanel.setSize(new Dimension(1, 1));
            innerPanel.setLayout(new WrapLayout());
            for (int j = 0; j < bookGroup.get(k).getNumberOfSections(); j++) {
                Section s = bookGroup.get(k).getSection(j);
                JPanel group = new JPanel();
                group.setName(s.getName());
                JCheckBox box = new JCheckBox();
                box.setText(s.getName());
                group.add(box);
                JFormattedTextField date = new JFormattedTextField(new SimpleDateFormat("mm/dd/yyyy"));
                date.setEditable(false);
                if (s.getCompletionDate() != null) {
                    date.setText(s.getCompletionDate().getMonth() + "//" + s.getCompletionDate().getDate() + "//" + s.getCompletionDate().getYear());
                }
                group.add(date);
                innerPanel.add(group);
            }
            jPanel.add(innerPanel);
            JSeparator separator = new JSeparator();
            separator.setVisible(true);
            jPanel.add(separator);
        }
        JScrollPane scrollPane = new JScrollPane(jPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setName(bookTabNames.get(i));
        tabs.add(scrollPane);
    }
}

Images before and after resizing

Edited 3 Years Ago by DarkLightning7

I think I'm doing something wrong with the BoxLayout, but I'm not sure. Anyone know what is called on window resize that would fix the layout as shown in the linked images above?

Here is the new simplified code:

public void setupBookTabs(JTabbedPane pane) {
        ArrayList<String> bookTabNames = getBookGroups();
        for (int i = 0; i < bookTabNames.size(); i++) {
            JPanel jPanel = new JPanel();
            BoxLayout layout = new BoxLayout(jPanel, BoxLayout.Y_AXIS);

            jPanel.setLayout(layout);
            jPanel.setName(bookTabNames.get(i));

            ArrayList<Book> bookGroup = getBooksByGroup(bookTabNames.get(i));
            for (int k = 0; k < bookGroup.size(); k++) {
                jPanel.add(bookGroup.get(k).getRenderable());
                jPanel.add(new JSeparator());
            }

            JScrollPane scrollPane = new JScrollPane(jPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scrollPane.setName(bookTabNames.get(i));
            scrollPane.getVerticalScrollBar().setUnitIncrement(12);

            pane.add(scrollPane.getName(), scrollPane);
        }
    }

Edited 3 Years Ago by DarkLightning7

here is the fix simply call scrollpane.validate(); after calling pane.add(scrollPane.getName(), scrollPane);

This question has already been answered. Start a new discussion instead.