It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

arranging Jlabel & JTextFiled

arranging Jlabel & JTextFiled

Postby Jose S on Sat May 12, 2007 8:31 am

Hi, I am learning how to use group layout in Swing. For example I have these labels and textFileds.

JLabel nameLabel = new JLabel("Name");
JLabel streetLabel = new JLabel("Street");
JLabel suburbLabel = new JLabel("Suburb");
JLabel phoneLabel = new JLabel("Phone");
JLabel emailLabel = new JLabel("Email");

name = new JTextField(20);
street = new JTextField(40);
suburb = new JTextField(20);
phone = new JTextField(20);
email = new JTextField(20);
setFieldsEditable(false);

Now to arrange it using GroupLayout.SequentialGroup class as following part of code, the question is that I don't understand the concept to add the components horizontally and vertically. Could anybody give me some suggestions?

// Create a sequential group for the horizontal axis.
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();

// The sequential group in turn contains two parallel groups.
// One parallel group contains the labels, the other the text fields.
hGroup.addGroup(layout.createParallelGroup().addComponent(nameLabel)
.addComponent(streetLabel).addComponent(suburbLabel).addComponent(
phoneLabel).addComponent(emailLabel));
hGroup.addGroup(layout.createParallelGroup().addComponent(name)
.addComponent(street).addComponent(suburb).addComponent(phone)
.addComponent(email));
layout.setHorizontalGroup(hGroup);

// Create a sequential group for the vertical axis.
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();

vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
.addComponent(nameLabel).addComponent(name));

vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
.addComponent(streetLabel).addComponent(street));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
.addComponent(suburbLabel).addComponent(suburb));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
.addComponent(phoneLabel).addComponent(phone));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
.addComponent(emailLabel).addComponent(email));
layout.setVerticalGroup(vGroup);
Jose S
 
Posts: 4
Joined: Tue May 08, 2007 5:50 pm

Who is online

Users browsing this forum: No registered users and 0 guests