Solved

Placing components on a frame

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 …
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

864 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

21 Experts available now in Live!

Get 1:1 Help Now