GUIGUI My name is Nnamdi. I am a new member in this forum. I am learning Java programming and how to develop Java GUI application. In my learning process, I have encountered the challenge stated below.

My question:

I am writing an application to properly align the GUI component exactly the way it appears in the attached file here but my application is not displaying the GUI exactly the way it is. Please I will be glad if someone can go through the code i have written for this which is shown here and correct me where I did not get it right. You can run my code to see exactly how my own code displays. I want it to be properly aligned to display exactly like the attached picture below. I will welcome hearing from members soonest.

Thank you.

GUI

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.Box;
import java.awt.Container;

public class GUISample extends JFrame{
private Container c;
private JCheckBox checkbox1;
private JCheckBox checkbox2;
private JLabel label1;
private JLabel label2;
private JButton button1;
private JButton button2;
private JButton button3;
private JTextField textfield1;
private JTextField textfield2;
private JPanel panelN;
private JPanel panelW;
private JPanel panelC;
private JPanel panelS;
private JPanel panelE;
public GUISample(){
super("GUI SAMPLE");
panelN = new JPanel();
panelW = new JPanel();
panelC = new JPanel();
panelS = new JPanel();
panelE = new JPanel();
c = getContentPane();
c.setLayout(new BorderLayout());
panelN.setLayout(new GridLayout(1,1));
panelW.setLayout(new GridLayout(2,1));
panelC.setLayout(new GridLayout(2,2));
panelE.setLayout(new GridLayout(3,1));
panelS.setLayout(new GridLayout(1,1));
checkbox1 = new JCheckBox("Snap to Grid");
checkbox2 = new JCheckBox("Show Grid");
label1 = new JLabel("X:");
label2 = new JLabel("Y:");
textfield1 = new JTextField("8",10);
textfield2 = new JTextField("8",10);
button1 = new JButton("OK");
button2 = new JButton("CANCEL");
button3 = new JButton("HELP");
c.add(panelN,BorderLayout.NORTH);
c.add(panelW,BorderLayout.WEST);
c.add(panelC,BorderLayout.CENTER);
c.add(panelE,BorderLayout.EAST);
c.add(panelS,BorderLayout.SOUTH);
panelW.add(checkbox1);
panelW.add(checkbox2);
panelC.add(label1);
panelC.add(textfield1);
panelC.add(label2);
panelC.add(textfield2);
panelE.add(button1);
panelE.add(button2);
panelE.add(button3);
}

public static void main (String []args){

GUISample gs = new GUISample();
gs.setSize(300,150);
gs.setVisible(true);
gs.setResizable(false);
gs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

i have done some moduifucations to your code
here is the your modified code

public class SampleGUI extends javax.swing.JFrame {

    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;

    public SampleGUI() {
        initComponents();
    }   

    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Ok");

        jButton2.setText("Cancel");

        jButton3.setText("Help");

        jCheckBox1.setText("Snap To Grid");

        jCheckBox2.setText("Show Grid");

        jLabel1.setText("X  :");

        jLabel2.setText("Y  :");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jCheckBox2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jCheckBox1, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField2, 0, 0, Short.MAX_VALUE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCheckBox1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCheckBox2)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3)))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        pack();
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SampleGUI().setVisible(true);
            }
        });
    }
}

happy coding

radha krishna .p

radhakrishna.p

Here at DaniWeb we try to help people learn Java and develop their Java skills. Your post explains and teaches nothing. In future please help by pointing people in the right direction - eg tell them why their code is going wrong, or which classes and methods they should read about. If you feel you should correct someone's code then that's useless if you don't explain what you changed and why you changed it as you did. If you need to explain with actual code then explain why you coded it the way you did. Don't just spoon-feed them a solution to copy and paste.

Thank you all.
@jalpesh, the link you gave was very helpful to me.
@radhakrishna.p,your code also served as a guide to me though I was trying to understand how to code grouplayout manually without an IDE.I have really studied this topic and solve even more complex example. However, I am still learning.
@JamesCherrill, I will appreciate if u can make correction to my code with detail explanation. It will improve my knowledge on this topic.

Thank you all.

This article has been dead for over six months. Start a new discussion instead.