Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Placing components on a frame

Posted on 2001-06-12
1
Medium Priority
?
171 Views
Last Modified: 2010-03-31
Hello,

        I have to place 3 labels and 3 text boxes on a frame for user input using Java Swing

         I have chosen Gridbaglayout and placed them as a combination of label and text box in each row. But the problem as the length of all the labels is not the same the placement of 3 text boxes are not same in the column..

        How to do this in swing...

bye,
Sobhan
0
Comment
Question by:sobhan_genie
[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 15

Accepted Solution

by:
ozymandias earned 80 total points
ID: 6181845
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class GridBagExample2 extends JFrame{

     Container contentPane;
     JLabel jl1;
     JLabel jl2;
     JLabel jl3;
     JTextField jtf1;
     JTextField jtf2;
     JTextField jtf3;

     public GridBagExample2(String args[]){
          super("GridBagExample2");
          this.setDefaultCloseOperation(EXIT_ON_CLOSE);
          contentPane = this.getContentPane();
          GridBagLayout gridbag1 = new GridBagLayout();
          contentPane.setLayout(gridbag1);
          GridBagConstraints  c  =  new  GridBagConstraints();
          c.fill  =  GridBagConstraints.NONE;
          Insets i = new Insets(2,2,2,2);
          c.insets = i;

          c.weightx = 0.5;
          c.weighty = 0.3;
          c.gridwidth = 1;
          c.gridheight = 1;

          c.gridx = 0;
          c.gridy = 0;
          c.anchor = GridBagConstraints.EAST;

          jl1 = new JLabel("Label 1 : ");
          gridbag1.setConstraints(jl1, c);
          contentPane.add(jl1);

          c.gridx = 1;
          c.anchor = GridBagConstraints.WEST;

          jtf1 = new JTextField(20);
          gridbag1.setConstraints(jtf1, c);
          contentPane.add(jtf1);


          c.gridx = 0;
          c.gridy = 1;
          c.anchor = GridBagConstraints.EAST;

          jl2 = new JLabel("Label 2 : ");
          gridbag1.setConstraints(jl2, c);
          contentPane.add(jl2);

          c.gridx = 1;
          c.anchor = GridBagConstraints.WEST;

          jtf2 = new JTextField(20);
          gridbag1.setConstraints(jtf2, c);
          contentPane.add(jtf2);

          c.gridx = 0;
          c.gridy = 2;
          c.anchor = GridBagConstraints.EAST;

          jl3 = new JLabel("Label 3 : ");
          gridbag1.setConstraints(jl3, c);
          contentPane.add(jl3);

          c.gridx = 1;
          c.anchor = GridBagConstraints.WEST;

          jtf3 = new JTextField(20);
          gridbag1.setConstraints(jtf3, c);
          contentPane.add(jtf3);

     }

     public static void main(String args[]){
          GridBagExample2 gbc = new GridBagExample2(args);
          gbc.setSize(400,200);
          gbc.setVisible(true);
     }
}
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

715 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