Hello, I need to build the following GUI
b1ec49424775312369c320b6b57981d8

and as usual I was hoping to have some advice before I start building it. In particular, I wonder if you guys think I should have any JPanel at all or if it is better to attach everything to the JFrame directly using the GridBagLayout (incidentally, is this the best layout manager to use in this case? I suspect somebody will say yes... :-)! )
Again no functionality is required (this is probably the last one I build without event handlers), so just the layout.
thanks

Start by loooking at which controls line up with which others - aligned controls are usually best on the same panel, unaligned ones may be easier in separate panels.Eg in the center there are CBs and JBs in a clear grid, but the "Print quality" line below doesn't line up with that, nor do the buttons to the right...
Try printing out a few copies and drawing panels and grids over it to see what works, before thinking about code.

  • GridBagLayout for JPanel into JFrame.CENTER, there JLabel (Printer: ...) must fills 5 columns in 1st row,

  • GridLayout(int rows, int cols, int hgap, int vgap) for JPanel.WEST/EAST

OK thanks guys. I have used photoshop and coloured and labelled the different areas that I think should be enclosed in different panels. It's actually harder than it seems, and I am not sure whether the following is correct. There will be 6 columns and 4 rows. I ended up with 4 panels:
1)optional: contains the Printer: my printer label and spans across 5 columns and only 1 row;
2)contains the text areas, the check boxes and the radio buttons and spans 5 cols and only 1 row;
3)Contains the print quality label, the combobox and the print to file check box. It spans 5 cols but only 1 row
4)contains the buttons. It spans 1 column and 4 rows altough it is not aligned with any of the previous ones
What do you reckon?
Here is the visual:
701e0b83e24f6e7fae8aadc8ea123fab
thanks

Don't know about "correct", but if I were doing this, that's how I would do it (at least as a starting point).
Looks like panel 1 has only one JLabel it it (?) so is redundant.

Thanks, sure sorry, you're right, the first it's not needed. I started to build it but then I have realized something: the GridBagLayout will be the layout manager of the JFrame to position the various panels. What type of layout should I instead use for each panel? I think that JPanel3 could have a flowLayout because elements flow from left to right whereas in panel 2 and panel 4 the elements are vertically stack, so perhaps - as mKorbel suggested a gridLayout?
thanks

That all sounds sensible. Just give it a go and see how it works. The simplest solution that works is the best.

Cool. Just something else occurred to me sorry. Do you know when I create the JTextArea objects textArea1 = new JTextArea(); how do I know how many columns and rows should it span? I mean take the first text area: it clearly spans 3 rows but if I include this in the constructor would that really make it span 3 rows the way it looks on the screenshot?
thanks

In the constructor you can specify a size in rows/columns of text, but there's nothing that specifies its size in relation to the other components. That's the job of the layout manager.

Hi there,
It took me ages to do this, sorry I got caught into other things.
So, let’s start with a general description. In the end I decided for a 6x7 grid with the top OK button in the top cell and the Help button in the bottom cell.
The programs compiles fine but at execution time I get the following

C:\Users\antonio.borrillo\Documents\useful\JAVA\GUI\14.11>java FormTest
Exception in thread "main" java.lang.NullPointerException
at java.util.Hashtable.hash(Unknown Source)
at java.util.Hashtable.put(Unknown Source)
at java.awt.GridBagLayout.setConstraints(Unknown Source)
at Form.<init>(Form.java:298)
at FormTest.main(FormTest.java:9)

I had a look around my code but I don't understand where the error is and I dont' understand what the error means. in FormTest.java I correctly created an object of type Form. In Form.java al line 298 layout.setConstraints( helpButton, constraints );there is nothing suspicious.
Here is the code:

/*Form.java
ex 14.11 p 662
*/
import java.awt.GridBagLayout;//chosen JFrame layout
import java.awt.GridBagConstraints;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.ButtonGroup;//for radio buttons
import java.awt.GridLayout;//chosen layout for panel selectionPanel and buttonsPanel
import java.awt.FlowLayout;//chosen layout for panel printQualityPanel
import javax.swing.JPanel;

public class Form extends JFrame{   
    //label
    private JLabel printerLabel;
    private JLabel printQualityLabel;

    //txt areas
    private JTextArea textArea1;
    private JTextArea textArea2;
    private JTextArea textArea3;

    //checkboxes
    private JCheckBox imageCheckBox;
    private JCheckBox textCheckBox;
    private JCheckBox codeCheckBox;
    private JCheckBox printCheckBox;

    //radio buttons
    private JRadioButton selectionRadioButton;
    private JRadioButton allRadioButton;
    private JRadioButton appletRadioButton;
    private ButtonGroup radioGroup;//to create logical connection between radio buttons

    //jcombobox
    private JComboBox<String> qualityCombo;
    private String[] values;

    //buttons
    private JButton okButton;
    private JButton cancelButton;
    private JButton setupButton;
    private JButton helpButton;

    //panels
    private JPanel selectionPanel;//contains text areas, radio buttons and chekboxes
    private JPanel printQualityPanel;//contains print quality combo box 
    private JPanel buttonsPanel;//contains buttons

    //layout
    private GridBagLayout layout;
    /* private GridLayout selectionPanelLayout;
    private GridLayout buttonsPanel; */
    private FlowLayout jPanel1Layout;//panel 1
    private FlowLayout jPanel2Layout;//panel 2
    private FlowLayout jPanel3Layout;//panel 3


    //constraints
    private GridBagConstraints constraints;

    //constructor
    public Form(){
        super("Form GUI");
        layout = new GridBagLayout(); 
        constraints = new GridBagConstraints();
        setLayout( layout );//set layout of JFrame

        //create panels
        selectionPanel = new JPanel();
        printQualityPanel = new JPanel();
        buttonsPanel = new JPanel();

        //create flow layout for aligning panels one next to the other
        jPanel1Layout = new FlowLayout();
        jPanel2Layout = new FlowLayout();
        jPanel3Layout = new FlowLayout();

        //create controls
        //labels
        printerLabel = new JLabel("Printer: My printer");
        printQualityLabel = new JLabel("Print Quality:");
        //txt areas
        textArea1 = new JTextArea( 15, 10 );
        textArea2 = new JTextArea( 15, 10 );
        textArea3 = new JTextArea( 15, 10 );
        //checkboxes
        imageCheckBox = new JCheckBox("Image");
        textCheckBox = new JCheckBox("Image");
        codeCheckBox = new JCheckBox("Image");
        printCheckBox = new JCheckBox("Image");
        //radio buttons
        selectionRadioButton = new JRadioButton("Selection", false);
        allRadioButton = new JRadioButton("All", true);
        appletRadioButton = new JRadioButton("Applet", false);
        //add radio buttons to the group
        radioGroup = new ButtonGroup();
        radioGroup.add( selectionRadioButton );
        radioGroup.add( allRadioButton );
        radioGroup.add( appletRadioButton );
        //combo box
        String[] values = { "Value1", "Value2", "Value3", "Value4" };
        qualityCombo = new JComboBox<>( values );
        //buttons
        okButton = new JButton("OK");
        cancelButton = new JButton("Cancel");
        setupButton = new JButton("Setup");
        okButton = new JButton("OK");

        //BASED ON A 6X7 GRID!!

        //add first label directly to the jframe
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 0;//COL position
        constraints.gridy = 1;//ROW position
        constraints.gridwidth = 1;//takes 1 col
        constraints.gridheight = 2;//takes 2 rows
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        layout.setConstraints( printerLabel, constraints );//set constraints on the element
        add( printerLabel );//add to JFrame

        //FIRST PANEL containing txt areas, radio buttons and check boxes
        //first text area
        constraints.fill = GridBagConstraints.VERTICAL;
        constraints.gridx = 0;//COL position
        constraints.gridy = 2;//ROW position
        constraints.gridwidth = 1;//takes 1 col
        constraints.gridheight = 3;//takes 3 rows
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        layout.setConstraints( textArea1, constraints );//set constraints on the element
        selectionPanel.add( textArea1 );//add component to jpanel
        //second txt area
        constraints.fill = GridBagConstraints.VERTICAL;
        constraints.gridx = 2;//COL position
        constraints.gridy = 2;//ROW position
        constraints.gridwidth = 1;//takes 1 col
        constraints.gridheight = 3;//takes 3 rows
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        layout.setConstraints( textArea2, constraints );//set constraints on the element
        selectionPanel.add( textArea2 );//add component to jpanel
        //third text area
        constraints.fill = GridBagConstraints.VERTICAL;
        constraints.gridx = 4;//COL position
        constraints.gridy = 2;//ROW position
        constraints.gridwidth = 1;//takes 1 col
        constraints.gridheight = 3;//takes 3 rows
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        layout.setConstraints( textArea3, constraints );//set constraints on the element
        selectionPanel.add( textArea3 );//add component to jpanel


        //checkboxes
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 1;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        layout.setConstraints( imageCheckBox, constraints );
        selectionPanel.add( imageCheckBox );//add component to jpanel

        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 1;
        constraints.gridy = 3;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        layout.setConstraints( textCheckBox, constraints );
        selectionPanel.add( textCheckBox );//add component to jpanel

        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 1;
        constraints.gridy = 4;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        layout.setConstraints( codeCheckBox, constraints );
        selectionPanel.add( codeCheckBox );//add component to jpanel

        //radio buttons
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 3;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        layout.setConstraints( selectionRadioButton, constraints );
        selectionPanel.add( selectionRadioButton );//add component to jpanel

        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 3;
        constraints.gridy = 3;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        layout.setConstraints( allRadioButton, constraints );
        selectionPanel.add( allRadioButton );//add component to jpanel

        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 3;
        constraints.gridy = 4;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        layout.setConstraints( appletRadioButton, constraints );
        selectionPanel.add( appletRadioButton );//add component to jpanel       
        //END OF FIRST PANEL

        //SECOND PANEL containing combo box and print to file
        //label
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 0;//COL position
        constraints.gridy = 5;//ROW position
        constraints.gridwidth = 1;//takes 1 col
        constraints.gridheight = 2;//takes 2 rows
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        layout.setConstraints( printQualityLabel, constraints );//set constraints on the element
        printQualityPanel.add( printQualityLabel );//add component to jpanel

        //jcombo
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 3;//COL position
        constraints.gridy = 5;//ROW position
        constraints.gridwidth = 2;//takes 1 col
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized     
        layout.setConstraints( qualityCombo, constraints );//set constraints on the element
        printQualityPanel.add( qualityCombo );//add component to jpanel

        //bottom checkbox
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 4;
        constraints.gridy = 5;
        constraints.gridwidth = 2;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        layout.setConstraints( printCheckBox, constraints );
        printQualityPanel.add( printCheckBox );//add component to jpanel        
        //END OF SECOND PANEL

        //THIRD PANEL
        //buttons
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 6;
        constraints.gridy = 0;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        layout.setConstraints( okButton ,constraints );
        buttonsPanel.add( okButton );//add component to jpanel

        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 6;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        layout.setConstraints( cancelButton ,constraints );
        buttonsPanel.add( cancelButton );//add component to jpanel

        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 6;
        constraints.gridy = 4;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        layout.setConstraints( setupButton, constraints );
        buttonsPanel.add( setupButton );//add component to jpanel

        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 6;
        constraints.gridy = 6;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        layout.setConstraints( helpButton, constraints );
        buttonsPanel.add( helpButton );//add component to jpanel

        //END OF THIRD PANEL

        //set layout of JPanels
        selectionPanel.setLayout( jPanel1Layout ); //set FlowLayout of Jpanel
        printQualityPanel.setLayout( jPanel2Layout );
        buttonsPanel.setLayout( jPanel2Layout );
        //add JPanels to JFrame
        add( selectionPanel );//add panel to JFrame
        add( printQualityPanel );
        add( buttonsPanel );

    }//end of constructor


}//end of class

and FormTest.java

/*FormTest.java
testing Form.java class
*/
import javax.swing.JFrame;
import java.awt.Dimension;

public class FormTest{
    public static void main( String[] args ){
        Form formTest = new Form();
        formTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        formTest.setVisible( true );
        Dimension minimumSize = new Dimension( 400, 200 );
        formTest.setMinimumSize( minimumSize );
        formTest.pack();
    }//end of main
}//end of class

So in essence I have first of all created all the components, then created the layouts needed (GridBagLayout to arrange the components and flowLayout to arrange the JPanels one after the other), set and added the constraints, attached the components to the relevant JPanels and attached the Jpanels to the JFrame. The top label - as suggested previously - is not inside any panel but it is attached directly to the JFrame.
Any idea what I have missed? I went over the code quite a few times but I think that an extra pair of eyes might spot the mistake!
thanks

You didn't initialise the helpButton variable, so it's null.

Duh! I checked the code twice, sorry not sure how I missed that...
Anyway, that sorted the output isn't that freat I am afraid...
4ca1942b3630bb6e21e2e1d153e62995
Umm...it's a bit like it's ignoring all my constraints...

I didn't have time to read all the code, but at first sight it looks like you have a FlowLayout inside each of your panels, so within the panels the FlowLayout will understand nothing of your GradBag constraints, and the components just go left to right.
The comment on line 79 seems to show some confusion - a container's layout controls what is inside the container, not how that container is aligned with others.

Oh..well the idea was to have a GridBagLayout to position elements inside the JPanels and Flow layout to position the panels themselves. SO I thought that to achieve that I needed 3 flowLayout objects (because there are 3 JPanels)...oh wait...getting confused now. So, to achieve the above how many GridBagLayout objects do I need?

OK I see what you mean now, I have done something really stupid. I will have another look and post the results...

Every container (JFrame, JPanel etc) has a layout manager. That manager lays out (positions and sizes) the components that are direct children of that container.
So the JFrame's manager lays out (controls the size and position of, but not the contents of) the textfield and the 2 panels that are its direct children.
The JPanel's managers lay out the radio buttons etc that are their direct children.

ps: strictly speaking, when you set a layout manager or add children to a JFrame, those are passed on to the JFrame's content pane. In practice you can ignore this most of the time, and treat it as if it was the JFrame that has the layout manager and child components.

Yes sorry, I effectively gave the jframe a GridBagLayout and the JPanels a flowLayout. What a noob! Sorry. OK now I have made some changes and created 1 GridLayoutBag object for each panel (+ 1 for the controller that is directly added to the JFrame) to lay out the content of the JPanel and created 1 flowLayout object to control the layout of the panels. Now, even if it still looks really bad, at least it's picking up something:
afcaba465392a58f56a5a2b265d9a2d5
Now I have to determine why it's al messed up. Here is the code just for reference.
One question. As I said I have in total 4 GridBagLayout objects, but I have only 1 constraint of type GridBagConstraints. Is that correct? (Sadly the examples I looked at - both on my book and on the API - don't use JPanels in combination with GridBagLayout).

/*Form.java
ex 14.11 p 662
*/
import java.awt.GridBagLayout;//chosen JFrame layout
import java.awt.GridBagConstraints;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.ButtonGroup;//for radio buttons
import java.awt.GridLayout;//chosen layout for panel selectionPanel and buttonsPanel
import java.awt.FlowLayout;//chosen layout for panel printQualityPanel
import javax.swing.JPanel;

public class Form extends JFrame{   
    //label
    private JLabel printerLabel;
    private JLabel printQualityLabel;

    //txt areas
    private JTextArea textArea1;
    private JTextArea textArea2;
    private JTextArea textArea3;

    //checkboxes
    private JCheckBox imageCheckBox;
    private JCheckBox textCheckBox;
    private JCheckBox codeCheckBox;
    private JCheckBox printCheckBox;

    //radio buttons
    private JRadioButton selectionRadioButton;
    private JRadioButton allRadioButton;
    private JRadioButton appletRadioButton;
    private ButtonGroup radioGroup;//to create logical connection between radio buttons

    //jcombobox
    private JComboBox<String> qualityCombo;
    private String[] values;

    //buttons
    private JButton okButton;
    private JButton cancelButton;
    private JButton setupButton;
    private JButton helpButton;

    //panels
    private JPanel selectionPanel;//contains text areas, radio buttons and chekboxes
    private JPanel printQualityPanel;//contains print quality combo box 
    private JPanel buttonsPanel;//contains buttons

    //layout
    private GridBagLayout jPanel1Layout;
    private GridBagLayout jPanel2Layout;
    private GridBagLayout jPanel3Layout;
    private GridBagLayout labelLayout;
    /* private GridLayout selectionPanelLayout;
    private GridLayout buttonsPanel; */
    private FlowLayout layout;//panel 1
/*  private FlowLayout jPanel2Layout;//panel 2
    private FlowLayout jPanel3Layout;//panel 3 */


    //constraints
    private GridBagConstraints constraints;

    //constructor
    public Form(){
        super("Form GUI");
        layout = new FlowLayout();
        setLayout( layout );//set layout of JFrame
        /* layout = new GridBagLayout(); 
        constraints = new GridBagConstraints();
        setLayout( layout );//set layout of JFrame */

        //create panels
        selectionPanel = new JPanel();
        printQualityPanel = new JPanel();
        buttonsPanel = new JPanel();

        //create flow layout for aligning panels one next to the other
        jPanel1Layout = new GridBagLayout();
        jPanel2Layout = new GridBagLayout();
        jPanel3Layout = new GridBagLayout();
        labelLayout = new GridBagLayout();
        constraints = new GridBagConstraints();

        //create controls
        //labels
        printerLabel = new JLabel("Printer: My printer");
        printQualityLabel = new JLabel("Print Quality:");
        //txt areas
        textArea1 = new JTextArea( 15, 10 );
        textArea2 = new JTextArea( 15, 10 );
        textArea3 = new JTextArea( 15, 10 );
        //checkboxes
        imageCheckBox = new JCheckBox("Image");
        textCheckBox = new JCheckBox("Image");
        codeCheckBox = new JCheckBox("Image");
        printCheckBox = new JCheckBox("Image");
        //radio buttons
        selectionRadioButton = new JRadioButton("Selection", false);
        allRadioButton = new JRadioButton("All", true);
        appletRadioButton = new JRadioButton("Applet", false);
        //add radio buttons to the group
        radioGroup = new ButtonGroup();
        radioGroup.add( selectionRadioButton );
        radioGroup.add( allRadioButton );
        radioGroup.add( appletRadioButton );
        //combo box
        String[] values = { "Value1", "Value2", "Value3", "Value4" };
        qualityCombo = new JComboBox<>( values );
        //buttons
        okButton = new JButton("OK");
        cancelButton = new JButton("Cancel");
        setupButton = new JButton("Setup");
        helpButton = new JButton("Help");
        okButton = new JButton("OK");

        //BASED ON A 6X7 GRID!!

        //add first label directly to the jframe
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 0;//COL position
        constraints.gridy = 1;//ROW position
        constraints.gridwidth = 1;//takes 1 col
        constraints.gridheight = 2;//takes 2 rows
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        labelLayout.setConstraints( printerLabel, constraints );//set constraints on the element
        add( printerLabel );//add to JFrame

        //FIRST PANEL containing txt areas, radio buttons and check boxes
        //first text area
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 0;//COL position
        constraints.gridy = 2;//ROW position
        constraints.gridwidth = 1;//takes 1 col
        constraints.gridheight = 3;//takes 3 rows
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        jPanel1Layout.setConstraints( textArea1, constraints );//set constraints on the element
        selectionPanel.add( textArea1 );//add component to jpanel
        //second txt area
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 2;//COL position
        constraints.gridy = 2;//ROW position
        constraints.gridwidth = 1;//takes 1 col
        constraints.gridheight = 3;//takes 3 rows
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        jPanel1Layout.setConstraints( textArea2, constraints );//set constraints on the element
        selectionPanel.add( textArea2 );//add component to jpanel
        //third text area
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 4;//COL position
        constraints.gridy = 2;//ROW position
        constraints.gridwidth = 1;//takes 1 col
        constraints.gridheight = 3;//takes 3 rows
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        jPanel1Layout.setConstraints( textArea3, constraints );//set constraints on the element
        selectionPanel.add( textArea3 );//add component to jpanel


        //checkboxes
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 1;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        jPanel1Layout.setConstraints( imageCheckBox, constraints );
        selectionPanel.add( imageCheckBox );//add component to jpanel

        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 1;
        constraints.gridy = 3;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        jPanel1Layout.setConstraints( textCheckBox, constraints );
        selectionPanel.add( textCheckBox );//add component to jpanel

        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 1;
        constraints.gridy = 4;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        jPanel1Layout.setConstraints( codeCheckBox, constraints );
        selectionPanel.add( codeCheckBox );//add component to jpanel

        //radio buttons
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 3;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        jPanel1Layout.setConstraints( selectionRadioButton, constraints );
        selectionPanel.add( selectionRadioButton );//add component to jpanel

        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 3;
        constraints.gridy = 3;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        jPanel1Layout.setConstraints( allRadioButton, constraints );
        selectionPanel.add( allRadioButton );//add component to jpanel

        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 3;
        constraints.gridy = 4;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        jPanel1Layout.setConstraints( appletRadioButton, constraints );
        selectionPanel.add( appletRadioButton );//add component to jpanel       
        //END OF FIRST PANEL

        //SECOND PANEL containing combo box and print to file
        //label
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 0;//COL position
        constraints.gridy = 5;//ROW position
        constraints.gridwidth = 1;//takes 1 col
        constraints.gridheight = 2;//takes 2 rows
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        jPanel2Layout.setConstraints( printQualityLabel, constraints );//set constraints on the element
        printQualityPanel.add( printQualityLabel );//add component to jpanel

        //jcombo
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 3;//COL position
        constraints.gridy = 5;//ROW position
        constraints.gridwidth = 2;//takes 1 col
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized     
        jPanel2Layout.setConstraints( qualityCombo, constraints );//set constraints on the element
        printQualityPanel.add( qualityCombo );//add component to jpanel

        //bottom checkbox
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 4;
        constraints.gridy = 5;
        constraints.gridwidth = 2;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        jPanel2Layout.setConstraints( printCheckBox, constraints );
        printQualityPanel.add( printCheckBox );//add component to jpanel        
        //END OF SECOND PANEL

        //THIRD PANEL
        //buttons
        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 6;
        constraints.gridy = 0;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        jPanel3Layout.setConstraints( okButton ,constraints );
        buttonsPanel.add( okButton );//add component to jpanel

        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 6;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        jPanel3Layout.setConstraints( cancelButton ,constraints );
        buttonsPanel.add( cancelButton );//add component to jpanel

        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 6;
        constraints.gridy = 4;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        jPanel3Layout.setConstraints( setupButton, constraints );
        buttonsPanel.add( setupButton );//add component to jpanel

        constraints.fill = GridBagConstraints.NONE;
        constraints.gridx = 6;
        constraints.gridy = 6;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0;//it should not expand when resized
        constraints.weighty = 0;//it should not expand when resized
        jPanel3Layout.setConstraints( helpButton, constraints );
        buttonsPanel.add( helpButton );//add component to jpanel

        //END OF THIRD PANEL

        //set layout of JPanels
        selectionPanel.setLayout( jPanel1Layout ); //set FlowLayout of Jpanel
        printQualityPanel.setLayout( jPanel2Layout );
        buttonsPanel.setLayout( jPanel3Layout );
        //add JPanels to JFrame
        add( selectionPanel );//add panel to JFrame
        add( printQualityPanel );
        add( buttonsPanel );

    }//end of constructor


}//end of class

You are setting GridBag constraints for your printer label, but that's a child of the JFrame (flow layout!), so they will be ignored.

Personally, I never use the shared constraints object approach, so I don't know what traps it may hide. I would be suspicious of this kind of code:

constraints.set.... some value
add componet using constraints
constraints.set... some different value
pack(); // which constraint value will this use???

Try breaking this down and doing one bit at a time. Start with just one of the JPanels and get that right, then do the rest.

thanks, it's that I don't know any better, meaning the API has that approach too, add(component, constraint), I wouldn't know how to separate the two. Is there a better way to add the component and separately set the constraints on a component, in other words to split this add(component, constraint)?

Also about the printer label: OK I now understand why the constraints are ignore, thanks for pointing that out. Isn't it worth adding the label to a JPanel so I can use GridBagLayout and all the constraints as opposed to attempt to position it using a flowLayout?

I have always specified the constraints right in the add statement, eg

add(comboBox, new GridBagConstraints(0, 0, 3, 1, 0.0, 0.0, CENTER, BOTH, new Insets(0, 6, 0, 6), 0, 0));

I like the way that all the layout info is right there in one obvious place.

Printer label: With just one component a panel is redundant.

OK I see I will do that then and try out one panel at time.
Yep, I know that the panel is redundant the only reason why I suggested that is because I am a bit worried that by positioning it with the FlowLayout I might not be able to move it to a position as precise as the one I could get with the GridBagLayout

OK, in that case use a GridBagLayout for the JPanel as well.
But don't get your grids confused. The frame will have a grid for the label and jpanels, then each panel will have its own internal grid for its children. There's no connection between any two of those grids.

Oh hang on you mean using a GridBagLayout for the JFrame?

Ooops yes, sorry. my typo.

OK no problem. Just one question about your method. I looked up the GridBagConstraints constructor just to check what order the parameters follow and here is what I have found:

public GridBagConstraints(int gridx, 
int gridy,
int gridwidth,
int gridheight,
double weightx,
double weighty,
int anchor,
int fill,
Insets insets,
int ipadx,
int ipady
)

One thing isn't clear: all the parameters are optional correct? So I can include all the above or a few, correct?
thanks

No, in Java method and constructor call parameters are all mandatory (except for the a last parameter marked ...).
Personally I like GridBagConstraints like that. It makes me think about each of those options, even if only for a moment, and that helps me get the best possible layout when the fonts change etc.

OK, thanks. I am halfway but the compiler is complaining quite a lot about "CENTER", "NONE", and "new Insets". Is there any other import declaration that I need to include?

I have changed the code to what you have suggested, it is much shorter now, but I have done that just where the components attach to the various JPanels. Do I have to do the same thing when I add the JPanels to the JFrame, correct?

import java.awt.GridBagLayout;//chosen JFrame layout
import java.awt.GridBagConstraints;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.ButtonGroup;//for radio buttons
import java.awt.GridLayout;//chosen layout for panel selectionPanel and buttonsPanel
import java.awt.FlowLayout;//chosen layout for panel printQualityPanel
import javax.swing.JPanel;

public class Form extends JFrame{   
//label
private JLabel printerLabel;
private JLabel printQualityLabel;

//txt areas
private JTextArea textArea1;
private JTextArea textArea2;
private JTextArea textArea3;

//checkboxes
private JCheckBox imageCheckBox;
private JCheckBox textCheckBox;
private JCheckBox codeCheckBox;
private JCheckBox printCheckBox;

//radio buttons
private JRadioButton selectionRadioButton;
private JRadioButton allRadioButton;
private JRadioButton appletRadioButton;
private ButtonGroup radioGroup;//to create logical connection between radio buttons

//jcombobox
private JComboBox<String> qualityCombo;
private String[] values;

//buttons
private JButton okButton;
private JButton cancelButton;
private JButton setupButton;
private JButton helpButton;

//panels
private JPanel selectionPanel;//contains text areas, radio buttons and chekboxes
private JPanel printQualityPanel;//contains print quality combo box 
private JPanel buttonsPanel;//contains buttons

//layout
private GridBagLayout jPanel1Layout;
private GridBagLayout jPanel2Layout;
private GridBagLayout jPanel3Layout;
private GridBagLayout labelLayout;
private GridBagLayout layout;//layout of JFrame

//constraints
private GridBagConstraints constraints;

//constructor
public Form(){
super("Form GUI");
layout = new GridBagLayout();
setLayout( layout );//set layout of JFrame  

//create panels
selectionPanel = new JPanel();
printQualityPanel = new JPanel();
buttonsPanel = new JPanel();

//create flow layout for aligning panels one next to the other
jPanel1Layout = new GridBagLayout();
jPanel2Layout = new GridBagLayout();
jPanel3Layout = new GridBagLayout();
labelLayout = new GridBagLayout();
constraints = new GridBagConstraints();

//create controls
//labels
printerLabel = new JLabel("Printer: My printer");
printQualityLabel = new JLabel("Print Quality:");
//txt areas
textArea1 = new JTextArea( 5, 5 );
textArea2 = new JTextArea( 5, 5 );
textArea3 = new JTextArea( 5, 5 );
//checkboxes
imageCheckBox = new JCheckBox("Image");
textCheckBox = new JCheckBox("Image");
codeCheckBox = new JCheckBox("Image");
printCheckBox = new JCheckBox("Print to file");
//radio buttons
selectionRadioButton = new JRadioButton("Selection", false);
allRadioButton = new JRadioButton("All", true);
appletRadioButton = new JRadioButton("Applet", false);
//add radio buttons to the group
radioGroup = new ButtonGroup();
radioGroup.add( selectionRadioButton );
radioGroup.add( allRadioButton );
radioGroup.add( appletRadioButton );
//combo box
String[] values = { "Value1", "Value2", "Value3", "Value4" };
qualityCombo = new JComboBox<>( values );
//buttons
okButton = new JButton("OK");
cancelButton = new JButton("Cancel");
setupButton = new JButton("Setup");
helpButton = new JButton("Help");
okButton = new JButton("OK");   

//BASED ON A 6X7 GRID!!     

//first text area       
selectionPanel.add( textArea1, new GridBagConstraints( 0, 2, 1, 3, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0 ) ) );//set constraints on the element and add to jpanel

//second txt area       
selectionPanel.add( textArea2, new GridBagConstraints( 2, 2, 1, 3, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0 ) ) );//add component to jpanel and set constraints

//third text area       
selectionPanel.add( textArea3, new GridBagConstraints( 4, 2, 1, 3, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0 ) ) );//add component to jpanel and add constraints


//checkboxes        
selectionPanel.add( imageCheckBox, new GridBagConstraints( 1, 2, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and add constraints        
selectionPanel.add( textCheckBox, new GridBagConstraints( 1, 3, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and add constraints     
selectionPanel.add( codeCheckBox, new GridBagConstraints( 1, 4, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and add constraints

//radio buttons     
selectionPanel.add( selectionRadioButton, new GridBagConstraints( 3, 2, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraints     
selectionPanel.add( allRadioButton, new GridBagConstraints( 3, 3, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraints       
selectionPanel.add( appletRadioButton, new GridBagConstraints( 3, 4, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraints
//END OF FIRST PANEL

//SECOND PANEL containing combo box and print to file
//label     
printQualityPanel.add( printQualityLabel, new GridBagConstraints( 0, 5, 1, 2, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraints

//jcombo
printQualityPanel.add( qualityCombo, new GridBagConstraints( 3, 5, 2, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraints

//bottom checkbox       
printQualityPanel.add( printCheckBox, new GridBagConstraints( 5, 5, 2, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraints
//END OF SECOND PANEL

//THIRD PANEL
//buttons       
buttonsPanel.add( okButton, new GridBagConstraints( 6, 0, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0 ) ) );//add component to jpanel and constraints      
buttonsPanel.add( cancelButton, new GridBagConstraints( 6, 2, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraints       
buttonsPanel.add( setupButton, new GridBagConstraints( 6, 4, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraint     
buttonsPanel.add( helpButton, new GridBagConstraints( 6, 6, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraints

//END OF THIRD PANEL

//set layout of JPanels
selectionPanel.setLayout( jPanel1Layout ); //
printQualityPanel.setLayout( jPanel2Layout );
buttonsPanel.setLayout( jPanel3Layout );

//set up the grid for the JFrame: 
//1st panel (below jlabel) attached to JFrame
constraints.fill = GridBagConstraints.NONE;
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;//it should not expand when resized
constraints.weighty = 0;//it should not expand when resized
layout.setConstraints( selectionPanel, constraints );

//2nd panel
constraints.fill = GridBagConstraints.NONE;
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;//it should not expand when resized
constraints.weighty = 0;//it should not expand when resized
layout.setConstraints( printQualityPanel, constraints );

//3rd panel
constraints.fill = GridBagConstraints.NONE;
constraints.gridx = 1;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;//it should not expand when resized
constraints.weighty = 0;//it should not expand when resized
layout.setConstraints( buttonsPanel, constraints );

//add JPanels to JFrame
add( printerLabel, new GridBagConstraints( 0, 0 , 1, 1, 0, 0, CENTER, NONE, new Insets(0, 0, 0, 0) )  );//add print label directly to JFrame + constraints
add( selectionPanel );//add panel to JFrame
add( printQualityPanel );
add( buttonsPanel );



}//end of constructor


}//end of class

To avoid typing too much I

import static java.awt.GridBagConstraints.*;

.. which lets me use the named constraint constants without having to fully-qualify them every time.

Do I have to do the same thing when I add the JPanels to the JFrame, correct?

If the JFrame has a GridBagLayout then yes, of course.

OK, that didn't work. I have made the change you suggested and also I have done the same thing for JPanels, but I still get a lot of error. So here is the code - I used pastebin this time, maybe it's more readable http://pastebin.com/PBrYE8aM and I have attached an excerpt of the errors. Still the same thing, it picks on the constants for whatever reason
b6bb17d99ffbd2dbfaa92891d89954cb

What you did didn't work becuase it wasn't the the change I suggested! Notice the static keyword in the import?

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.