Search code examples
javaswinguser-interfacegrid-layout

Java - Problem during the creation of a stock JPanel


I am attempting to create a JPanel that houses in a GridLayout a number of JLabels to the left JTextFields on the right. Unfortunately, nothing is shown even if the Components are reported as correctly added. Where is the problem?

public class AlternateGL_JPanel extends JPanel {
    protected JPanel layoutPanel;
    protected int rows, columns;
    
    public AlternateGL_JPanel(int rows, int columns) { 
        this.rows       = rows;
        this.columns    = columns;
        
        // ===== MAIN FRAME DEFINITION =====
        setBorder(new EmptyBorder(0, 0, 0, 0));
        setLayout(new GridLayout(this.rows, this.columns));
    //  setBounds(10, 10, 500, 500);
        
        // ===== INNER PANEL =====
        this.layoutPanel = new JPanel();    //This is the nested panel
        layoutPanel.setLayout(new GridLayout(this.rows, this.columns));
        super.add(layoutPanel, BorderLayout.PAGE_START);
    }
    // =========================================================            
        // TODO | Superclass: JPanel
    
    public void add(JComponent component) {
        layoutPanel.add(component);
    }
    
}


/**  A <b>ManyTextAndInsertText</b> is a {@link JPanel} that houses a number of {@link JLabel}s to the left 
 * and {@link JTextField}s on the right.
 * 
 */
public class ManyTextAndInsertText extends AlternateGL_JPanel {
    private JLabel[] texts;
    private JTextField[] insertTexts;
    
    public ManyTextAndInsertText(String[] descriptions) {
        super(descriptions.length, 2);
        
        this.texts          = new JLabel[descriptions.length];
        this.insertTexts    = new JTextField[descriptions.length];
        
        for(int i=0 ; i<descriptions.length ; i++)
            {
            texts[i]        = new JLabel(descriptions[i]);
            insertTexts[i]  = new JTextField();

            for(int j=0 ; j<this.getComponentCount() ; j++)
                System.out.println("\t" + this.getComponent(j).toString());
            this.add(texts[i]);

            for(int j=0 ; j<this.getComponentCount() ; j++)
                System.out.println("\t" + this.getComponent(j).toString());
            this.add(insertTexts[i]);
            }
    }


public class TestManyTextes extends JFrame {
    

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestManyTextes frame = new TestManyTextes();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TestManyTextes() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new ManyTextAndInsertText(new String[] { "First text: " , "Second text: "});
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
    }




Solution

  • I greatly simplified your code and created this GUI.

    Text GUI

    Instead of extending Swing components, I used Swing components.

    You would get a nicer looking form using a GridBagLayout, rather than a GridLayout.

    Here's the runnable example.

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.GridLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.border.EmptyBorder;
    
    public class TestManyTexts {
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        new TestManyTexts();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public TestManyTexts() {
            JFrame frame = new JFrame("Test Many Texts");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            ManyTexts panel = new ManyTexts(new String[] { "First text: ", 
                    "Second text: ", "Third Text:", "Forth Text" });
            frame.add(panel.getPanel(), BorderLayout.CENTER);
    
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public class ManyTexts {
    
            private JPanel panel;
    
            private JTextField[] fields;
    
            public ManyTexts(String[] labels) {
                createPanel(labels);
            }
    
            private void createPanel(String[] labels) {
                panel = new JPanel(new GridLayout(0, 2));
                panel.setBorder(new EmptyBorder(5, 5, 5, 5));
    
                fields = new JTextField[labels.length];
    
                for (int i = 0; i < labels.length; i++) {
                    JLabel label = new JLabel(labels[i]);
                    panel.add(label);
    
                    fields[i] = new JTextField(15);
                    panel.add(fields[i]);
                }
            }
    
            public JPanel getPanel() {
                return panel;
            }
    
            public JTextField[] getFields() {
                return fields;
            }
    
        }
    
    }