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.


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");

public static void main (String []args){

GUISample gs = new GUISample();
Re: Java GUI Alignment Problem 80 80

You can use the GroupLayout provided by the java.

please follow this to get more information about it:


Re: Java GUI Alignment Problem 80 80

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() {

    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();





        jCheckBox1.setText("Snap To Grid");

        jCheckBox2.setText("Show Grid");

        jLabel1.setText("X  :");

        jLabel2.setText("Y  :");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                .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)
                .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))
                        .addGap(21, 21, 21)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))

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

happy coding

radha krishna .p

Re: Java GUI Alignment Problem 80 80


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.

Re: Java GUI Alignment Problem 80 80

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.

Be a part of the DaniWeb community

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