Solved

JLabels and JTextFields on GridBagLayout

Posted on 2004-03-30
1
1,198 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
1 Comment
 
LVL 5

Accepted Solution

by:
twobitadder earned 125 total points
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maven java path setting 5 49
base64 decode encode 12 96
backtracking recursion  code 19 39
mockito example issue 8 29
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now