0

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 300x300 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;


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


    public Main(){

        frameTitle = "CRANES QUOTATIONS";

        // MAIN INSTANCE VARIABLES DEFINITION
        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

        this.setTitle(frameTitle);

        JPanel panel = new JPanel();
        panel.setBackground(Color.red);
        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.add(panel);            
        // =========================================================================== 
        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(){

            @Override
            public void run() {
                new Main();

            }

        });


    }
    }
zypa
  • 303
  • 2
  • 13
  • I would suggest having a look at [A Visual Guide to Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) and [Laying Out Components Within a Container](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) – MadProgrammer Apr 13 '17 at 22:30
  • That is the first place where I have looked up. But there it does not mention the issue that Pavlo is stating. I hate it when logic gets overwritten by comportamental bugs.... It is hard to learn the right path. I will try this evening after I come back from work and hope it will work. – zypa Apr 14 '17 at 05:53
  • 1
    The other place to look would be the [JavaDocs for `JFrame`](https://docs.oracle.com/javase/8/docs/api/javax/swing/JFrame.html) which states *"The default content pane will have a BorderLayout manager set on it"* – MadProgrammer Apr 14 '17 at 06:29

1 Answers1

1

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.

Pavlo Viazovskyy
  • 927
  • 5
  • 12
  • 2
    [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – MadProgrammer Apr 13 '17 at 22:31
  • Yes, actually this worked. I have set up the FlowLayout to the JFrame then added the JPanels and setup the sizes using PreferredSize. All working for now. Thanks again – zypa Apr 18 '17 at 20:58