Ouahib
Ouahib

Reputation: 33

GridLayout size doesn't fill all space

I need your help for a project that I need to finish tomorrow. I'm trying to implement a tab which will put some value from database. But I'm facing with an issue that I can't resolve. I want to add several layout one by one. Each layout is splitted into 3 columns but these layout (and columns) doesn't take all available space as you can see on this screen:

enter image description here

Here's my code:

package pizzeria;

import java.sql.SQLException;

import javax.swing.*;

import java.awt.*;

public class CarteDesPizzas extends JPanel{

    static GridLayout grid1 = new GridLayout(1,1);

    static GridLayout grid2 = new GridLayout(1,3);

    static GridLayout grid3 = new GridLayout(1,1);

    public static JPanel affichageCarte() throws SQLException ,Exception {
        JLabel label = new JLabel("Carte des pizzas", JLabel.CENTER);
        label.setForeground(new Color(120, 90, 40));

        JPanel selection = new JPanel();
        GridBagLayout grid = new GridBagLayout();
        selection.setLayout(grid);
         
        JPanel header;
        JPanel content;
         
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1; 
        c.weighty = 0.1;
        c.gridy = 0;
         
        header = new JPanel(new GridBagLayout());
        header.add(label);
        selection.add(header,c);
         
        content = new JPanel();
        c.weightx = 1; 
        c.weighty = 0.9;
        c.gridy = 1;



        JPanel selection2 = new JPanel(grid2);

        JPanel pinkSelection = new JPanel();
        pinkSelection.setBackground(Color.PINK);

        JPanel redSelection = new JPanel();
        redSelection.setBackground(Color.RED);

        JPanel greenSelection = new JPanel();
        greenSelection.setBackground(Color.GREEN);

        selection2.add(pinkSelection);
        selection2.add(redSelection);
        selection2.add(greenSelection);

        content.add(selection2);

        selection.add(content,c);
        
        return selection;
    }
}

Thank you for your help

Upvotes: 0

Views: 167

Answers (2)

Zavier Rosenbaum
Zavier Rosenbaum

Reputation: 1

You are not using GridLayout although you declared such three variables. You are using GridBagLayout with some specific values that lead to what you get.

Upvotes: 0

Abra
Abra

Reputation: 20914

A JPanel is a container. It's purpose is to contain other Components.

GridBagLayout places components in rectangles (cells) in a grid, and then uses the components' preferred sizes to determine how big the cells should be.

A JPanel, by default, has no preferred size. Its size is determined by the sizes of the components it contains. Since the JPanels in your code contain no components, their preferred size is very small. Hence they appear very small in your GUI.

The simplest solution would be to explicitly assign a preferred size to the JPanel.

In the below code I only set the preferred size for pinkSelection.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

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

public class CarteDesPizzas extends JPanel {
    static GridLayout grid1 = new GridLayout(1,1);
    static GridLayout grid2 = new GridLayout(1,3);
    static GridLayout grid3 = new GridLayout(1,1);

    public static JPanel affichageCarte() {
        JLabel label = new JLabel("Carte des pizzas", JLabel.CENTER);
        label.setForeground(new Color(120, 90, 40));

        JPanel selection = new JPanel();
        GridBagLayout grid = new GridBagLayout();
        selection.setLayout(grid);
         
        JPanel header;
        JPanel content;
         
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1; 
        c.weighty = 0.1;
        c.gridy = 0;
         
        header = new JPanel(new GridBagLayout());
        header.add(label);
        selection.add(header,c);
         
        content = new JPanel();
        c.weightx = 1; 
        c.weighty = 0.9;
        c.gridy = 1;

        JPanel selection2 = new JPanel(grid2);

        JPanel pinkSelection = new JPanel();
        pinkSelection.setPreferredSize(new Dimension(100, 60));
        pinkSelection.setBackground(Color.PINK);

        JPanel redSelection = new JPanel();
        redSelection.setBackground(Color.RED);

        JPanel greenSelection = new JPanel();
        greenSelection.setBackground(Color.GREEN);

        selection2.add(pinkSelection);
        selection2.add(redSelection);
        selection2.add(greenSelection);

        content.add(selection2);

        selection.add(content,c);
        
        return selection;
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(affichageCarte());
                frame.pack();
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
}

Here is a screen capture:

screen capture

Upvotes: 1

Related Questions