'Why doesn't a panel with GridBagLayout show the content?

I wrote a little something here. It's working if I don't backPanel.setLayout(new GridBagLayout);

But without the grid bag, the content stays in the top left I maximise the screen.

With the grid bag I only get the red backPanel in the frame. Well, there is a gray pixel in the middle of the screen. I'm assuming that's my panel, but I can't make it bigger. I tried setSize but it doesn't change. Also, I had the panel.setBounds(0, 0, getWidth(),getHeight());. I'm not sure why I removed it.

My main is in the other file. The only thing it does at the moment is to call the LoginFrame.

Here is the code:

package first;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

public class LoginFrame extends JFrame implements ActionListener {

    private JTextField textField;
    private JPasswordField passwordField;

    public LoginFrame() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 300);

        JPanel backPanel = new JPanel(new GridBagLayout());
        backPanel.setBackground(Color.RED);
        
        JPanel panel = new JPanel();
        panel.setSize(500, 300);
        panel.setBackground(Color.LIGHT_GRAY);
        panel.setLayout(null);

        JLabel label;
        panel.add(label = new JLabel("Username:"));
        label.setBounds(20, 100, 100, 25);

        panel.add(textField = new JTextField());
        textField.setBounds(140, 100, 200, 25);

        panel.add(label = new JLabel("Password:"));
        label.setBounds(20, 145, 100, 25);

        panel.add(passwordField = new JPasswordField());
        passwordField.setBounds(140, 145, 200, 25);

        panel.add(label = new JLabel("CTC Bank"));
        label.setFont(new Font("New Times Roman", Font.BOLD, 50));
        label.setBounds(0, 0, getWidth(), 100);
        label.setHorizontalAlignment(JLabel.CENTER);

        JButton button;
        panel.add(button = new JButton("Login"));
        button.setBounds(140, 200, 100, 25);
        button.addActionListener(this);
        button = defaultActionKeyEnter(button, KeyEvent.VK_ENTER);

        panel.add(button = new JButton("Register"));
        button.setBounds(240, 200, 100, 25);
        button.addActionListener(this);
        button = defaultActionKeyEnter(button, KeyEvent.VK_ENTER);
        
        
        //add(panel);
        backPanel.add(panel);
        add(backPanel, BorderLayout.CENTER);
        revalidate();
        repaint();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static JButton defaultActionKeyEnter(JButton button, int desiredKeyCode) {

        InputMap inputMap = button.getInputMap(JComponent.WHEN_FOCUSED);

        KeyStroke spaceKeyPressed = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false);
        KeyStroke spaceKeyReleased = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true);

        KeyStroke desiredKeyPressed = KeyStroke.getKeyStroke(desiredKeyCode, 0, false);
        KeyStroke desiredKeyReleased = KeyStroke.getKeyStroke(desiredKeyCode, 0, true);

        inputMap.put(desiredKeyPressed, inputMap.get(spaceKeyPressed));
        inputMap.put(desiredKeyReleased, inputMap.get(spaceKeyReleased));

        inputMap.put(spaceKeyPressed, "none");
        inputMap.put(spaceKeyReleased, "none");

        return button;
    }
    
    // Unfinished code dont worry bout it...
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Login")) {
            if (textField.getText().equals("Heinz")
                    && (new String(passwordField.getPassword()).equals("password123"))) {
                // color = Color.GREEN;
            } else {
                JOptionPane.showMessageDialog(this, "Wrong Username or Password", "Error", JOptionPane.WARNING_MESSAGE);
                // color = Color.RED;
            }
        } else {
            JOptionPane.showMessageDialog(this, "Cya");
            dispose();
            setVisible(false);
        }
        // panel.setBackground(color);
    }
}

I have seen questions about this but none of the answers were helpful in my case.

Calling the following didn't help.

revalidate();
repaint();

Did I maybe add it in the wrong order?

And how does the code look like to you? Would you consider this clean?



Solution 1:[1]

The layout of backPanel will mis-calculate the dimensions of "panel" because "panel" does not participate in layout management properly, without a layout manager of its own.

One solution to this is to use setLayout(null) also on the "backPanel", or add "panel" directly to the JFrame.

With the first suggestion ("backPanel.setLayout(null);" just after it is created), plus the following main method:

    public static void main(String[] args) {
        new LoginFrame();
    }

I get this:

enter image description here

Sources

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

Source: Stack Overflow

Solution Source
Solution 1