?
Solved

JLabels and JTextFields on GridBagLayout

Posted on 2004-03-30
1
Medium Priority
?
1,212 Views
Last Modified: 2008-02-26
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
Comment
Question by:dkim18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 5

Accepted Solution

by:
twobitadder earned 500 total points
ID: 10716524
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question