Skip to content
Advertisement

Grid not working in java swing and getting overflowed

I have a Jframe and I want to show grids as follows:

https://i.stack.imgur.com/UmXPJ.png

There are 3rows and in the 2nd row there are two columns.So,but when I add a Jlabel and Jtextfield in 2nd row,then the 2nd column of 2nd grid goes below the 2nd row and acts as 3rd column. For eg:

https://i.stack.imgur.com/X0qUZ.png

So "hello55" needs to come side by side of the 2nd row,but it is coming downward as extra row in swing. How can I make it side to side?

So,I tried as:

public class AdminDashboard extends JFrame {
    
     
    private JPanel panel,subPanel1;
    
    public AdminDashboard() {
        System.out.println("hello");
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1000, 609);
        panel=new JPanel();
        panel.setLayout(new GridLayout(3,1,5,10));
        JLabel labe11=new JLabel("hello11");
        JLabel labe12=new JLabel("hello22");
        JLabel labe13=new JLabel("hello33");
        JLabel labe14=new JLabel("hello44");
        JLabel labe15=new JLabel("hello55");

        panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        panel.add(labe11);
        
        
        subPanel1 = new JPanel(); // sub-panel 1
        
        subPanel1.setLayout(new GridLayout(0,2,5,10));
        //creating form for 2nd row 1st column
        JLabel userLabel = new JLabel("User");
        userLabel.setBounds(10, 10, 80, 25);
        subPanel1.add(userLabel);

        JTextField userText = new JTextField(20);
        userText.setBounds(100, 10, 80, 25);
        subPanel1.add(userText);

        JLabel passwordLabel = new JLabel("Password");
        passwordLabel.setBounds(10, 40, 80, 25);
        subPanel1.add(passwordLabel);

        JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(100, 40, 160, 25);
        subPanel1.add(passwordText);

        JButton loginButton = new JButton("login");
        loginButton.setBounds(10, 80, 80, 25);
        subPanel1.add(loginButton);
        
        JButton registerButton = new JButton("register");
        registerButton.setBounds(180, 80, 80, 25);
        subPanel1.add(registerButton);
        
        //2nd row 2nd column
        subPanel1.add(labe15);
        
        
      
        subPanel1.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        subPanel1.setBackground(Color.red);
        panel.add(subPanel1);
        
        panel.add(labe13);
        

        add(panel);
    }

    
}

Advertisement

Answer

The below code simply sets up your desired GUI and nothing more. It is not a complete, working application. Explanations after the code.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class AdminDashboard implements Runnable {
    private JFrame frame;

    @Override
    public void run() {
        showGui();
    }

    private JPanel createBottomPanel() {
        JPanel bottomPanel = new JPanel();
        JLabel hello33 = new JLabel("hello33");
        bottomPanel.add(hello33);
        return bottomPanel;
    }

    private JPanel createFormPanel() {
        JPanel formPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.LINE_START;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets.bottom = 5;
        gbc.insets.left = 5;
        gbc.insets.right = 5;
        gbc.insets.top = 5;
        JLabel userLabel = new JLabel("User");
        formPanel.add(userLabel, gbc);

        gbc.gridx = 1;
        JTextField userTextField = new JTextField(6);
        formPanel.add(userTextField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        JLabel passwordLabel = new JLabel("Password");
        formPanel.add(passwordLabel, gbc);

        gbc.gridx = 1;
        JPasswordField passwordField = new JPasswordField(6);
        formPanel.add(passwordField, gbc);

        gbc.anchor = GridBagConstraints.CENTER;
        gbc.gridx = 0;
        gbc.gridy = 2;
        JButton loginButton = new JButton("login");
        formPanel.add(loginButton, gbc);

        gbc.gridx = 1;
        JButton registerButton = new JButton("register");
        formPanel.add(registerButton, gbc);

        return formPanel;
    }

    private JPanel createMainPanel() {
        JPanel mainPanel = new JPanel(new GridLayout(0, 2, 5, 10));
        mainPanel.add(createFormPanel());
        mainPanel.add(createTablePanel());
        return mainPanel;
    }

    private JPanel createTablePanel() {
        JPanel tablePanel = new JPanel();
        JLabel hello55 = new JLabel("hello55");
        tablePanel.add(hello55);
        return tablePanel;
    }

    private JPanel createTopPanel() {
        JPanel topPanel = new JPanel();
        JLabel hello11 = new JLabel("hello11");
        topPanel.add(hello11);
        return topPanel;
    }

    private void showGui() {
        frame = new JFrame("Admin Dashboard");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(createTopPanel(), BorderLayout.PAGE_START);
        frame.add(createMainPanel(), BorderLayout.CENTER);
        frame.add(createBottomPanel(), BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    /**
     * Start here.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new AdminDashboard());
    }
}

The default layout manager for the content pane of JFrame is BorderLayout. So I placed the hello11 as the top component and the hello33 as the bottom component.

In the center component I put a panel and set its layout manager to GridLayout so that I could nest within it two panels side by side where the left panel is your form and the right panel is hello55.

For the panel displaying the [login] form, I used GridBagLayout.

Here is a screen capture of the window displayed when running the above code.

Admin_Dashboard

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement