Results 1 to 2 of 2
  1. #1
    adeeb is offline Member
    Join Date
    May 2008
    Posts
    29
    Rep Power
    0

    Default "Problem With GroupLayout", Please Help !

    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);
    layout.setHorizontalGroup(layout.createSequentialG roup().addComponent(name).addComponent(tf)
    .addGroup(layout.createParallelGroup().addComponen t(age).addComponent(tf1))
    );
    layout.setVerticalGroup(layout.createParallelGroup ()
    .addGroup(layout.createParallelGroup().addComponen t(name).addComponent(tf).addComponent(age))
    .addComponent(tf1));

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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You have to think about both horizontal as well as vertical alignments in the layout.

    Java Code:
    import javax.swing.*;
    import static javax.swing.GroupLayout.Alignment.*;
    /**
     *
     * @author Eranga Tennakoon
     */
    public class GroupLayoutTest extends JFrame{
    
        public GroupLayoutTest() {
            JLabel name = new JLabel("Name");
            JLabel age = new JLabel("Age");
            JTextField tf = new JTextField(20);
            JTextField tf1 = new JTextField(20);
            
            GroupLayout layout = new GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setAutoCreateGaps(true);
            layout.setAutoCreateContainerGaps(true);
            
            layout.setHorizontalGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(LEADING)
                        .addComponent(name)
                        .addComponent(age))
                    .addGroup(layout.createParallelGroup(LEADING)
                        .addComponent(tf)
                        .addComponent(tf1))
                    );
            
            layout.setVerticalGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(BASELINE)
                        .addComponent(name)
                        .addComponent(tf))
                    .addGroup(layout.createParallelGroup(LEADING)
                        .addComponent(age)
                        .addComponent(tf1))
                    );
         
            setTitle("GroupLayout");
            pack();
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            // TODO code application logic here
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new GroupLayoutTest().setVisible(true);
                }
            });
        }
        
    
    }

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 09:26 AM
  2. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 03:42 AM
  3. "Displayed tab width" problem...
    By Petike in forum Eclipse
    Replies: 0
    Last Post: 03-17-2008, 09:39 PM
  4. I have a problem with variable "i"
    By silvia in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 11:05 PM
  5. Problem with "GregorianCalendar"
    By tola.ch2004 in forum New To Java
    Replies: 2
    Last Post: 07-12-2007, 08:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •