'JPanel Class with null layout not showing components

So, i created an object of class "CustomPanel" that creates a JPanel with a GridLayout and a label inside of it then I added it to my JFrame. It works fine showing the label "HELLO", but when I change the layout manager of the jpanel to (null) it doesn't show anything. I know, I know using null layout is a very bad practice but I just want to know why it isn't showing the components.

Main class:

import javax.swing.JFrame;

public class MainMenu extends javax.swing.JFrame{

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Size the window.
        frame.setSize(500, 500);

        CustomPanel panel = new CustomPanel();

        frame.getContentPane().add(panel);

        frame.setVisible(true);
    }

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

CustomPanel class with GridLayout (This works fine):

import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CustomPanel extends JPanel{

    public CustomPanel() {
        initUI();
    }

    public final void initUI() {

        // create the panel and set the layout
        JPanel main = new JPanel();
        main.setLayout(new GridLayout());

        // create the labels
        JLabel myLabel = new JLabel("HELLO");

        // add componets to panel
        main.add(myLabel);

        this.add(main);
    }
}

CustomPanel class with Null layout (This doesn't work):

import javax.swing.JLabel;
import javax.swing.JPanel;

public class CustomPanel extends JPanel{

    public CustomPanel() {
        initUI();
    }

    public final void initUI() {

        // create the panel and set the layout
        JPanel main = new JPanel();
        main.setLayout(null);

        // create the labels
        JLabel myLabel = new JLabel("HELLO");
        myLabel.setBounds(10, 10, myLabel.getPreferredSize().width, myLabel.getPreferredSize().height);

        // add componets to panel
        main.add(myLabel);

        this.add(main);
    }
}

The jlabel is correctly set inside the jpanel so it should be showing in the upper-left side of the jframe, but it doesn't. What is causing this? what am I missing?.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source