Search code examples
javaswingjpanellayout-managergridbaglayout

GridBagLayout anchor


I am trying to use Anchor for setting the alignment of my components. Here is my code:

public void intiConOpt()
{
    /*********************************** connection options ****************************************/
    
    conOptGBC.insets = new Insets(5, 5, 5, 5);
    conOptGBC.weightx = 1.0;
    conOptGBC.weighty = 1.0;
    conOptGBC.anchor = GridBagConstraints.NORTHWEST;
    conOpt = new JPanel();
    conOpt.setLayout(new GridBagLayout());
    
    //////////////////////////////////////////cycle time////////////////////////////////////////////
    
    readPeriodLabel = new JLabel("Node read cycle time:");
    conOptGBC.gridx = 0;
    conOptGBC.gridy = 0;
    conOpt.add(readPeriodLabel, conOptGBC);
    
    readPeriodNumberModel = new SpinnerNumberModel();
    readPeriodSpinner = new JSpinner(readPeriodNumberModel);
    JSpinner.NumberEditor readPeriodEditor = new JSpinner.NumberEditor(readPeriodSpinner,"###");
    readPeriodSpinner.setEditor(readPeriodEditor);
    readPeriodNumberModel.setValue(TopologyMain.settings.getSettingsList().get("nodeReadingCycleTime"));

    readPeriodSpinner.setPreferredSize(new Dimension(40, 20));
    conOptGBC.gridx = 1;
    conOptGBC.gridy = 0;
    conOpt.add(readPeriodSpinner, conOptGBC);
    
    readPeriodLabel2 = new JLabel("<html><font size=2>(4-180 sec)</font></html>");
    conOptGBC.gridx = 2;
    conOptGBC.gridy = 0;
    conOpt.add(readPeriodLabel2, conOptGBC);
    
    ///////////////////////////////////////////time out////////////////////////////////////////////
    
    cliTimeoutLabel = new JLabel("CLI response timeout:");
    conOptGBC.gridx = 0;
    conOptGBC.gridy = 1;
    conOpt.add(cliTimeoutLabel, conOptGBC);
    
    cliTimeoutNumberModel = new SpinnerNumberModel();
    cliTimeoutSpinner = new JSpinner(cliTimeoutNumberModel);
    JSpinner.NumberEditor cliTimeoutEditor = new JSpinner.NumberEditor(cliTimeoutSpinner,"###");
    cliTimeoutSpinner.setEditor(cliTimeoutEditor);
    cliTimeoutNumberModel.setValue(TopologyMain.settings.getSettingsList().get("cliTimeout") /10);

    cliTimeoutSpinner.setPreferredSize(new Dimension(40, 20));
    conOptGBC.gridx = 1;
    conOptGBC.gridy = 1;
    conOpt.add(cliTimeoutSpinner, conOptGBC);
    
    cliTimeoutLabel2 = new JLabel("<html><font size=2>(8-999 sec)</font></html>");
    conOptGBC.gridx = 2;
    conOptGBC.gridy = 1;
    conOpt.add(cliTimeoutLabel2, conOptGBC);
    
    ///////////////////////////////////////busy response//////////////////////////////////////////////
            
    cliBusySleepLabel = new JLabel("CLI busy check time:");
    conOptGBC.gridx = 0;
    conOptGBC.gridy = 2;
    conOpt.add(cliBusySleepLabel, conOptGBC);
    
    cliBusySleepNumberModel = new SpinnerNumberModel();
    cliBusySleepSpinner = new JSpinner(cliBusySleepNumberModel);
    JSpinner.NumberEditor cliBusySleepEditor = new JSpinner.NumberEditor(cliBusySleepSpinner,"###");
    cliBusySleepSpinner.setEditor(cliBusySleepEditor);
    cliBusySleepNumberModel.setValue(TopologyMain.settings.getSettingsList().get("cliBusySleep") /1000);
    
    cliBusySleepSpinner.setPreferredSize(new Dimension(40, 20));
    conOptGBC.gridx = 1;
    conOptGBC.gridy = 2;
    conOpt.add(cliBusySleepSpinner, conOptGBC);
    
    cliBusySleepLabel2 = new JLabel("<html><font size=2>(1-999 sec)</font></html>");
    conOptGBC.gridx = 2;
    conOptGBC.gridy = 2;
    conOpt.add(cliBusySleepLabel2, conOptGBC);
    conOpt.setVisible(false);
    contentPanel.add(conOpt);
}

My problem is the GridBagConstraints anchor has no effect, my components are still in the center of the panel.


Solution

  • The anchor property is working correctly, the issue you are having is with...

    conOptGBC.weightx = 1.0;
    conOptGBC.weighty = 1.0;
    

    The problem is, you've asked GridBagLayout to allocate equal amount of space for each column, so they will take up, in this case, are third each, this is what would be consider.

    By removing the conOptGBC.weightx = 1.0; you get...

    Form

    It's a little difficult to see, but if we add some guides in...

    Grid

    You can see that the fields begin to align to the north-west...