Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Placing components on a frame

Posted on 2001-06-12
1
Medium Priority
?
172 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
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: 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

In this post we will learn different types of Android Layout and some basics of an Android App.
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…
Suggested Courses

963 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