Search code examples
javaswingjtextfield

Cant add JTextfield onto JPanel in JTabbedPane


I'm trying to make a basic UI with Swing. I want to have a JFrame with a JTabbedPane with multiple Tabs. If I only make 1 JPanel with a JTextField it works fine, but as soon as I want to add another JPanel with a JTextField it just shows nothing. What am I doing wrong here?

Here is a quick example:

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

public class SwingUI {

    private final JFrame frame;
    private final JTabbedPane tabbedPane;

    public SwingUI(){

        frame = new JFrame("Test");
        frame.setSize(1000, 1000);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new GridLayout(1, 1));

        tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Login", makeLoginPanel());
        tabbedPane.addTab("Login2", makeLoginPanel());  //if this is left out it works
        frame.getContentPane().add(tabbedPane);

    }

    private JPanel makeLoginPanel(){
        JPanel p = new JPanel();
        p.setLayout(null);

        JLabel lblName = new JLabel("Name:");
        lblName.setBounds(50,50,100,30);
        p.add(lblName);

        JTextField x = new JTextField("text");
        x.setBounds(200,200,200,200);
        p.add(x);

        return p;
    }
}

Solution

  • The frame should be made visible AFTER the components have been added to the frame.

    So:

    frame.setVisible( true );
    

    should be the last statement in your constructor.

    Also:

    frame.setSize(1000, 1000);
    

    Don't hardcode sizes. You don't know what resolution other people may be using.

    Instead use:

    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    

    and the frame will open maximized on your screen.

    Also,

    frame.getContentPane().setLayout(new GridLayout(1, 1));
    

    Don't use a GridLayout. The default layout is the BorderLayout which will allow you to add a component to the CENTER by default and the component will fill the entire space of the frame.