• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1223
  • Last Modified:

JLabels and JTextFields on GridBagLayout

hi!

I am trying to arrange JLabels and JTextFields on GridBagLayout.

The format I want is like this:

      label1:textfield
      label2:textfield

but my code arrage like this:
     
      label1:  label2
     textfield textfield


*********************************
import java.awt.*;
import javax.swing.*;

class SizePanel
    extends JPanel {
  JLabel widthLabel;
  JLabel htLabel;
  JTextField wtTField;
  JTextField htTField;
  GridBagLayout layout;
  GridBagConstraints constraints;
  public SizePanel() {
    layout = new GridBagLayout();
    constraints = new GridBagConstraints();
    setLayout(layout);
    widthLabel = new JLabel("Graph Width in pixels:", SwingConstants.RIGHT);
    htLabel = new JLabel("Graph Height in pixels:", SwingConstants.RIGHT);
    wtTField = new JTextField(10);
    htTField = new JTextField(10);

    arrageComponent(widthLabel, 0, 0, 1, 1);
    constraints.anchor = GridBagConstraints.EAST;
    add(widthLabel, constraints);

    arrageComponent(wtTField, 1, 0, 1, 1);
    constraints.anchor = GridBagConstraints.WEST;
    add(htLabel, constraints);

    arrageComponent(wtTField, 0, 1, 1, 1);
    constraints.anchor = GridBagConstraints.EAST;
    add(wtTField, constraints);

    arrageComponent(wtTField, 1, 1, 1, 1);
    constraints.anchor = GridBagConstraints.WEST;
    add(htTField, 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;
  }

}
0
dkim18
Asked:
dkim18
1 Solution
 
twobitadderCommented:
arrageComponent(widthLabel, 0, 0, 1, 1);
    constraints.anchor = GridBagConstraints.EAST;
    add(widthLabel, constraints);

    arrageComponent(htLabel, 0, 1, 1, 1);
    constraints.anchor = GridBagConstraints.WEST;
    add(htLabel, constraints);

    arrageComponent(wtTField, 1, 0, 1, 1);
    constraints.anchor = GridBagConstraints.EAST;
    add(wtTField, constraints);

    arrageComponent(htField, 1, 1, 1, 1);
    constraints.anchor = GridBagConstraints.WEST;
    add(htTField, constraints);

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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