Search code examples
javaswingmodel-view-controller

Problems with BorderLayout in Java Swing


I'm making a little program for a project in Java, but I have some problems with the layout. I created 3 Panel and I wanted them to be like this:

My Wish

I tried with BorderLayout, setting 2 of the panels on north and 1 on center and it didn't work, I tried to make them Weast an East and the last in South, but didn't work.

Now I tried FlowLayout and it's not working for the 2 others panel, I have this: my result

I put the code of my View Package of this interfaces (we are using MVC), if someone could help me I would appreciate it. Thanks guys!

package View;

import Model.Client;
import Model.Magasin;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class Statistiques extends JFrame {

    JLabel article = new JLabel("Nombre article vendu:");
    JLabel chifre_affair = new JLabel("Chifre d'Affair:");
    JLabel art_plus_vendu = new JLabel("Article plus vendus:");
    JLabel nbTArticle = new JLabel("2");
    JLabel nbChiffreAffair = new JLabel("3");
    JLabel nomArticlePlusVendus = new JLabel("iPhone12");

    Magasin magasin;
    Vector<Client> listeClients = new Vector<Client>();

    JComboBox clients = new JComboBox<>(listeClients);
    JLabel espace = new JLabel();
    JLabel art_plus_achete = new JLabel("Article plus achete:");
    JLabel nomArticlePlusAchete = new JLabel();
    JLabel list_label = new JLabel("liste articles achete: ");



    JComboBox vendeurs = new JComboBox<>();
    JLabel art= new JLabel("Article plus achete:");
    JLabel nom = new JLabel();
    JLabel listv_label = new JLabel("liste articles achete: ");


    JPanel general_north = new JPanel();
    JPanel client_north = new JPanel();
    JPanel vendeurs_center = new JPanel();


    public Statistiques(Magasin m){
        this.setTitle("STATISTIQUES");
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setPreferredSize(new Dimension(800,800));
        this.setLayout(new BorderLayout());
        magasin = m;
        listeClients = magasin.listeClient;

        general_north.setLayout(new GridLayout(3,2,1,1));
        general_north.setSize(400,200);
        //general_north.setBorder(BorderFactory.createEmptyBorder(100,200,100,200));

        client_north.setLayout(new GridLayout(4,1,1,1));
        client_north.setPreferredSize(new Dimension(400,200));
        //client_north.setBorder(BorderFactory.createEmptyBorder(100,200,100,200));

        client_north.add(art_plus_achete);
        client_north.add(nomArticlePlusAchete);


        vendeurs_center.setLayout(new FlowLayout());
        vendeurs_center.setPreferredSize(new Dimension(800,400));

        getContentPane().add(general_north);
        getContentPane().add(client_north);
        getContentPane().add(vendeurs_center);

        general_north.add(article);
        general_north.add(nbTArticle);
        general_north.add(chifre_affair);
        general_north.add(nbChiffreAffair);
        general_north.add(art_plus_vendu);
        general_north.add(nomArticlePlusVendus);


        this.pack();
    }
}

Solution

  • You need to use a compound layout (several LayoutManagers). So use BorderLayout and add one JPanel as north and the vendeurs_center as center. Then inside the north panel use e.g. GridLayout and place two components general_north and client_north.

    As an alternative use GridBagLayout - that is one of the layouts that will achieve everything in one go:

    In one component configured with GridBagLayout add all three components like this:

    general_north: Position 1,1
    client_north: Position 2,1
    vendeurs_center: Position 1,2; spanX: 2
    

    The advantage is that here you have lots of additional options to play with. Most important in my eyes is the capability to define how the components should grow/shrink when the window gets resized.