Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Syntax for GroupLayout  RSS feed

 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need the complete syntax for GroupLayout, i want to add two text field one below the other.So any one can please tell me only the Syntax i.e exactly hoe to use grouplayouts. I am totally confused by its coding. So please some one help.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by adeeb alexander:
Hi,
I need the complete syntax for GroupLayout, i want to add two text field one below the other.So any one can please tell me only the Syntax i.e exactly hoe to use grouplayouts. I am totally confused by its coding. So please some one help.


This should help.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless this is an assignment (in which case you should be doing it yourself and not asking here), why use GroupLayout at all? From the Sun Java Tutorials: How to Use GroupLayout
GroupLayout is a layout manager that was developed for GUI builders ...

For just two components arranged vertically, BoxLayout or GridLayout looks to be a better choice.

edit I see pete stein already made the same recommendation 36 hours earlier.

If you continue with one thread, or at the very least provide a link to your earlier topic, people won't waste time suggesting what's already been suggested.
[ June 03, 2008: Message edited by: Darryl Burke ]
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
with all your help i was able to write the code. But now i am having a problem the code which i have written is not giving the desired output. i.e i wanted to place in first line a label and a text field and in second line also same a label and a text field. But i am getting all the four components in a line. Can any body give me the solution please. Here is my code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static javax.swing.GroupLayout.Alignment.*;

public class exp5 extends JFrame
{
private Container c;
//private GroupLayout gl;

public exp5()
{
super("Experiment5");
c = getContentPane();
JPanel panel = new JPanel();
JLabel name = new JLabel("Name");
JLabel age = new JLabel("Age");
JTextField tf = new JTextField(20);
JTextField tf1 = new JTextField(10);
GroupLayout layout = new GroupLayout(panel);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
GroupLayout.SequentialGroup hgroup = layout.createSequentialGroup();
hgroup.addGroup(layout.createSequentialGroup().addComponent(name).addComponent(age));
hgroup.addGroup(layout.createParallelGroup().addComponent(tf).addComponent(tf1));
layout.setHorizontalGroup(hgroup);

GroupLayout.SequentialGroup vgroup = layout.createSequentialGroup();
vgroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(name).addComponent(tf));
vgroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(age).addComponent(tf1));
layout.setVerticalGroup(vgroup);
getContentPane().add(panel);
setSize(600,600);
setVisible(true);

}
public static void main(String args[])
{
exp5 exp = new exp5();
exp.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!