Search code examples
javaswinglayout-managergridbaglayout

Components a "bit" out of position with GridBagLayout - Java Swing


I'm designing my first user interface completely from code by hand (with no use of any "drag and drop" IDE).

Therefore I've run into a problem with one form. As you can see here in my layout screenshot: my form layout

The textFields in red are not aligned with the From radio button. Same problem with the textfields in blue not aligned with the label "To:". They look like they are not aligned by one line but I'm using the same value for gridy, in the "From:" radio button and "To:" label.

The ones in green are slightly not aligned as well.

I've been all over my code but I can't find where's the mistake. Could someone help me out on aligning them properly?

EDIT

public void layoutComponents()
{
    //add to the layout (what is above )
    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints(); //class that specifies where goes what we want



    ////////// Insert and Align Labels and Fields //////////


    gc.weightx = 1; // how much space it takes relatively to other cells
    gc.weighty = 1; // how much space it takes relatively to other cells
    gc.fill = GridBagConstraints.NONE;

    //specify to wich side it will stick to
    gc.anchor = GridBagConstraints.LINE_START; // stick to the right hand side

    Insets fivePixels = new Insets(0, 0, 0, 5);// adds 5 pixels of spcae to the right
    Insets zeroPixels = new Insets(0, 0, 0, 0);

    gc.gridx = 0;
    gc.gridy = 0; //y increase downwards
    gc.insets = fivePixels;
    add(dataSourceBoldLabel, gc);

    gc.gridy = 1; //second row
    add(sourceTableLabel, gc);

    gc.gridy = 2;
    add(processDataPeriodLabel, gc);

    gc.gridy = 5;
    add(resultsBoldLabel, gc);

    gc.gridy = 6;
    add(writeResultsToNewTableRadio,gc);

    gc.gridy = 7;
    add(writeResultsToExistingTableRadio, gc);

    gc.gridy = 8;
    add(processDataOptionsLabel, gc);

    gc.gridy = 9;
    add(accessSessionTimeThresholdLabelLabel, gc);

    gc.gridy = 10;
    add(transitionTimeThresholdLabelLabel, gc);







    ///////////     Second Column  X = 1     ////////////


    gc.insets = zeroPixels;
    gc.gridx = 1;


    gc.gridy = 1;
    add(sourceTables, gc);

    gc.gridy = 2;
    add(allRadio, gc);

    gc.gridy = 3;
    add(fromRadio, gc);

    gc.gridy = 4;
    gc.anchor = GridBagConstraints.LINE_END;
    add(toLabel, gc);

    gc.anchor = GridBagConstraints.NORTHWEST;
    gc.gridy = 6;
    add(newTableField, gc);

    gc.gridy = 7;
    add(resultsTables, gc);

    gc.gridy = 9;
    add(accessSessionTimeThresholdField,gc);

    gc.gridy = 10;
    add(transitionTimeThresholdField, gc);

    gc.anchor = GridBagConstraints.NORTHWEST; //push the buttom to the top
    gc.gridy = 15;
    add(start, gc);



    ////////////////// Third Column    X = 2 //////////////////
    gc.gridx = 2;
    gc.gridy = 4;
    add(fromDateField, gc);

    gc.gridy = 5;
    add(toDateField, gc);





    ///////////////// 4th Column    X = 3 /////////////////////
    gc.gridx = 3;
    gc.gridy = 4;
    add(fromTimeField, gc);

    gc.gridy = 5;
    add(toTimeField, gc);


}

}


Solution

  • I've found the problem.

    It was on the gc.anchor = GridBagConstraints.X where X is the direction. I was using different directions when I should keep it constant or match both columns and line with the correct direction.