Solved

Placing components on a frame

Posted on 2001-06-12
1
167 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 20 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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

680 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