Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with GroupLayout  RSS feed

 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i am having a problem with the code which i have written. It 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.Please point where to modify it.



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);
}
});
}
}

 
Nikos Pougounias
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GroupLayout is too complex to use it directly.

Please try the GridLayout.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. I have already used GridLayout but i really want to learn about GroupLayout. I know to and components using grouplayout as i have given the program before you can see in it. But i want to arrange the components as i like how to do that. javascript: x()
banghead
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!