Search code examples
javaarraysswingjframejlabel

How to mannually add a array of JLabel to JFrame


I've been trying to make an array of labels displayed on JFrame. But JFrame couldn't display the created labels. It just appears blank. How to fix this problem?

public static void main(String args[]) {
    String s = "This is a sample sentence.";
    String[] words = s.split("\\s+");

    JLabel[] jLblWords=new JLabel[words.length];

    /* create labels */

    for(int i=0;i<words.length;i++){
        jLblWords[i]=new JLabel(words[i]);

      }

 java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
 }

Solution

    • JFrame has BorderLayout implemented in API, in BorderLayout only one JComponents can be placed to one of 5th areas

    • your code posted here isn't completed, doesn't shows how is arrays of JLabels added to JFrame

    • to start with GridLayout


    • I'd be suggest to use JTable (in JScrollPane) instead of bunch of JLabels added to JFrame