Solved

JLabels and JTextFields on GridBagLayout

Posted on 2004-03-30
1
1,203 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 125 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example for saving data 19 77
eclipse buid path vs tomcat lib path 10 38
How  can  i  resolve  HTTP Status 404 -? 8 59
Java class and jar 3 42
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

749 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