Hi guys, I tought I'd experiment a bit with layout managers, trying to get to grips with them. I will start with a flowLayout, then hopefully move on to more complex ones.
OK, so here is the code for the first example, very simple one:

/*FlowLayoutManager.java
layout manager tests*/
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JCheckBox;

public class FlowLayoutManager extends JFrame{
    private JTextField textField1;
    private JTextField textField2; 
    private JCheckBox checkBox1; 
    private JCheckBox checkBox2;
    private FlowLayout layout;
    public FlowLayoutManager(){
        super( "Flow Layout" );
        //creating and setting layout for controllers
        layout = new FlowLayout();
        setLayout( layout );
        //creating controllers
        textField1 = new JTextField( "", 10 );
        textField2 = new JTextField( "", 10 );
        checkBox1 = new JCheckBox( "checkbox 1" );
        checkBox2 = new JCheckBox( "checkbox 2" );
        //attaching controllers to JFrame window
        add( textField1 );
        add( textField2 );
        add( checkBox1 );
        add( checkBox2 );

    }//end of constructor

}//end of class

and test class

/*testing FlowLayoutManager.java
FlowLayoutManagerTest.java
*/
import javax.swing.JFrame;
public class FlowLayoutManagerTest{

    public static void main( String args[] ){
        FlowLayoutManager FlowLayoutManagerTest = new FlowLayoutManager();
        FlowLayoutManagerTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        //FlowLayoutManagerTest.setSize( 400, 200 );
        FlowLayoutManagerTest.setVisible( true );
        FlowLayoutManagerTest.pack();
    }//end of main
}//end of class

This produces this layout:
d1a7291bc096d871c826d4ded98363f4
Now, it's OK but the height of the window is not reakky what I want. How do I increase that to a more user friendly size?
Also, you will notice in the FlowLayoutManagerTest.java that I have commented out //FlowLayoutManagerTest.setSize( 400, 200 );
Although I have been kindly warned already, if I uncomment that line and comment out this instead FlowLayoutManagerTest.pack();, the layout breaks in that the last checkbox is not aligned left by default as it should in a flowLayout, see screenshot
24a059a0bf8ff3b62271b1599bb1decd
Why is that and what is the best way to fix it? Set a minimum size?

You can work with setMinimumSize to have a minimum height and width, (and pack(), of course), but that gives you no control over the width or where it will flow onto a new line. setMaximumSize may allow you to force it onto a new line, but the size is in pixels, where the comonents will vary in width according to the fonts used, so it's not very useful.

IMHO FlowLayout is so simplistic that it's basically useless.

Rather than start with "how do I do this with XxxLayout?", start with a sketch of your desired layout, then look at the visual guide to layout managers and pick the one that's closest to the layout you want, using JPanels to create sub-layouts where necessary.

thanks JamesCherrill, yes it is really simplistic you're right, in fact I don't think I will ever use it, but because I am still at the very early stage of GUI I thought I'd go through a few different layout manager, no matter how easy/useless they are. This is just so I get to grips with the whole GUI thing.

I added a min size and that seems to have done the trick for reasons that I don't quite understand as yet

/*testing FlowLayoutManager.java
FlowLayoutManagerTest.java
*/
import javax.swing.JFrame;
import java.awt.Dimension;//to sort specify the dimension
public class FlowLayoutManagerTest{

    public static void main( String args[] ){
        FlowLayoutManager FlowLayoutManagerTest = new FlowLayoutManager();
        FlowLayoutManagerTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        //FlowLayoutManagerTest.setSize( 400, 200 );        
        Dimension minimumSize = new Dimension(400, 250);//specify dimensions
        FlowLayoutManagerTest.setMinimumSize( minimumSize );//set dimensions

        FlowLayoutManagerTest.setVisible( true );
        FlowLayoutManagerTest.pack();
    }//end of main
}//end of class

Thanks for the link, very useful!

Is that some confusion over the class name and the variable name? You declare a class FlowLayoutManagerTest but then in the main method it looks like FlowLayoutManager is a class and FlowLayoutManagerTest is a variable. The Java convention is for variable names to begin with a lower-case letter, and it would certainly make it easier for everyone to read your code quickly if you stick to the conventions.

Apologies, yes you're right I forgot to use the lower-case for the variable, thanks for pointing that out, I will amend

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.