Solved

JLabels and JTextFields on GridBagLayout

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

724 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