Solved

Placing components on a frame

Posted on 2001-06-12
1
163 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

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

16 Experts available now in Live!

Get 1:1 Help Now