Search code examples
javaswinglayoutjlabelborder-layout

Trying to create multiple JLabels, however only one is appearing


I am trying to create multiple JLabels of the same form and then trying to add them to the same JPanel. However, only one of the JLabels appears and I can't figure out why! Here is the code that I have written:

    final JPanel labelPanel = new JPanel(new BorderLayout());
    panel.add(labelPanel, BorderLayout.NORTH);

    JLabel[] dashedLineLabel = new JLabel[wordLength];

    for (int i = 0; i < wordLength; i++)
    {   
        dashedLineLabel[i] = new JLabel("__  ");
        dashedLineLabel[i].setFont(new Font("Serif", Font.BOLD, 30));
        labelPanel.add(dashedLineLabel[i]);
    }   

Any help would be greatly appreciated! Thank you


Solution

  • You aren't using the BorderLayout properly. The labels are all added at the center location of the layout, and thus overwrite each others. Try a FlowLayout instead, or even better, a MigLayout.