[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

arranging labels and textfields inside of border by using GBL layout

hi!

I need put some JLabels and JTextFields inside of border the following manner.

   label_1:  textField
   label_2:  testField
   ...
   ...

Somehow, mine just arrange all horizontally.
I try to change around,  but didn't help.
How do I correct that?
**********************************
  public void createSinDataPanel() {
    sinDataPanel = new JPanel();
    GridBagLayout layout = new GridBagLayout();
    sinDataPanel.setLayout(layout);

    sinCheckBox = new JCheckBox("Sine plot");
    constraints = new GridBagConstraints();

    arrageComponent(sinCheckBox, 0, 0, 1, 1);
    constraints.insets = new java.awt.Insets(2, 2, 2, 2);
    constraints.anchor = constraints.WEST;
    sinDataPanel.add(sinCheckBox, constraints);
//----------------------
    JPanel inputPanel = new JPanel();
    inputPanel.setBorder( BorderFactory.createTitledBorder("Sine parameters") );
    arrageComponent(inputPanel, 0, 1, 1, 1);
    sinDataPanel.add(inputPanel, constraints);

    JLabel ampLabel = new JLabel("Amplitude: ");
    JLabel periodLabel = new JLabel("Period: ");
    JLabel minXLabel = new JLabel("Min X: ");
    JLabel maxXLabel = new JLabel("Max X: ");
    JLabel cgcLabel = new JLabel("Current Graph Color: ");

    ampTField = new JTextField("1.0", 5);
    periodTField = new JTextField("1.0", 5);
    minXSinTField = new JTextField("0.0", 5);
    maxXSinTField = new JTextField("1.0", 5);
                                                                           //--------------------labels and textfields inside of border
    arrageComponent(ampLabel, 0, 1, 1, 1);
    constraints.anchor = GridBagConstraints.WEST;
    inputPanel.add(ampLabel, constraints);

    arrageComponent(ampTField, 1, 1, 1, 1);
    constraints.anchor = GridBagConstraints.EAST;
    inputPanel.add(ampTField, constraints);

    arrageComponent(periodLabel, 0, 2, 1, 1);
    constraints.anchor = GridBagConstraints.WEST;
    inputPanel.add(periodLabel, constraints);

    arrageComponent(periodTField, 1, 2, 1, 1);
    constraints.anchor = GridBagConstraints.EAST;
    inputPanel.add(periodTField, constraints);

...
...
}

*****************************
  private void arrageComponent(Component component, int column, int row,
                               int width, int height) {
    constraints.gridx = column;
    constraints.gridy = row;

    constraints.gridwidth = width;
    constraints.gridheight = height;
    constraints.anchor = GridBagConstraints.LINE_END;
  }
0
dkim18
Asked:
dkim18
  • 3
1 Solution
 
zzynxSoftware engineerCommented:
You'll never learn it ;)

inputPanel.setLayout( new GridBagLayout() );

before adding items to it
0
 
zzynxSoftware engineerCommented:
When you do

new JPanel()

the panel has a *FlowLayout by default* (which explains the horizontal layout)
0
 
dkim18Author Commented:
I wasn't realizing that simple mistake...
you are right..java  is rather straightforward...

Thanks,
0
 
zzynxSoftware engineerCommented:
You're welcome.
Thanks for accepting
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now