Skip to content

JPanel takes the entire space of JFrame

I am struggling to place some simple objects using the FlowLayout manager in Swing.

Basically I have created a small program, I have set up the size of the frame, then crated some JLabels and then one JPanel.

I have put the JPanel a setSize of 300×300 and changed it’s color to red to visually see it. However it takes up the entire space of the JFrame and it seems that the setsize has no effect on it.

What am I doing wrong? I tried googling it and changed the code several time, but no luck. I even read, but clearly not understood correctly, the code on oracle… Please help. Underneath is my code so far.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Main extends JFrame{

    public String frameTitle, projectNameString, countryNameString, projectDetailString, requestDateString;
    private int offerRevisionVal;
    private Toolkit toolkit;

    //private JLabel projectLabel, countryLabel, projectDetailLabel, offerLabel, requestDateLabel, emptyLabel;

    public Main(){

        frameTitle = "CRANES QUOTATIONS";

        projectNameString = "Project Title"; // TO ADD LATER 
        countryNameString = "Brazil"; // TO ADD LATER
        projectDetailString = "32 Cranes biTravi"; // TO ADD LATER
        offerRevisionVal = 01; // TO ADD LATER
        requestDateString = "20-April-2017"; // tTO ADD LATER


        JPanel panel = new JPanel();
        panel.setSize(new Dimension(300,300));

        //projectLabel = new JLabel(projectNameString);
        //countryLabel = new JLabel(countryNameString);
        //projectDetailLabel = new JLabel(projectDetailString);
        //offerLabel = new JLabel(String.valueOf(offerRevisionVal));
        //requestDateLabel = new JLabel(requestDateString);

        // =========================================================================== 
        this.setSize(800, 300); // set the height and width of my window
        this.centerToScreen ();
        this.setVisible(true); // set visibility 
        this.setDefaultCloseOperation(EXIT_ON_CLOSE); // sets up what it does when I close the App.


    // Method to center to screen
    public void centerToScreen (){

        toolkit = getToolkit();
        Dimension size = toolkit.getScreenSize(); // gets the screen size
        setLocation(size.width / 2 - getWidth() / 2, size.height / 2 - getHeight() / 2); // sets the location

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){

            public void run() {
                new Main();






JFrame uses BorderLayout as default LayoutManager. So you need to implicitly set layout of your JFrame to FlowLayout:

this.setLayout(new FlowLayout());

You also need to use setPreferredSize() instead of setSize() on your panel, because setSize() has no effect if container of your panel (JFrame) has non-null LayoutManager.