0

I am getting insane! I am adding a JPanel to an another JPanel by a method. This method is generating a grid via SpringLayout. The problem is that after adding all JComponents to the panel the preferred size of the panel is 0, thus no content is displayed. Using an another layout manager solves the visibility but I need the structures of the SpringLayout. I can't find my problem.

private JPanel panelErzeugen(ArrayList<JComponent> labels){
    JPanel panel = new JPanel();

    SpringLayout layout = new SpringLayout();
    panel.setLayout(layout);

    int abstandVert = 5;
    int abstandHori = 5;

    for(JComponent label : labels){
        if(labels.indexOf(label) == labels.size()/2){
            abstandVert = 100;
            abstandHori = 5;
        }

        layout.putConstraint(SpringLayout.WEST, panel, abstandVert, SpringLayout.WEST, label);
        layout.putConstraint(SpringLayout.NORTH, panel, abstandHori, SpringLayout.NORTH, label);

        panel.add(label);

        abstandHori = abstandHori + 25;
    }

    return panel;
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jul 19 '16 at 18:43
  • 1
    I need to understand something: The `label` gets created in the `for-each`-loop and never gets assigned anything. Therefore you're adding an empty label to an empty panel, correct? Or am I missing some parts of your code that you have not yet posted? – hamena314 Jul 20 '16 at 07:51
  • The labels were created by an another method. As you can see, the method gets an ArrayList filled with JComponents – smokerec Jul 20 '16 at 20:18
  • Tip: Add @smokerec (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Jul 23 '16 at 04:19

0 Answers0