0

I am trying to add two JPanels, which has specific works to do, into another JPanel. These two JPanels are created using GroupLayout layout manager. Now I have to add these two JPanels into another JPanel using the same GroupLayout layout manager. Based on this example, here's what I've done so far.

Panel1:

    package exercise1;

    import java.awt.Color;
    import javax.swing.BorderFactory;
    import javax.swing.GroupLayout;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;

    /**
     *
     * @author Prashanth
     */
    public class GroupPanel1 extends JPanel
    {
        private JLabel label1 = new JLabel("Primary:");
        private JTextField field1 = new JTextField(16);
        private JLabel label2 = new JLabel("Secondary:");
        private JTextField field2 = new JTextField(16);
        private JLabel label3 = new JLabel("Tertiary:");
        private JTextField field3 = new JTextField(16);
        private JRadioButton male=new JRadioButton("Male", false);
        private JRadioButton female=new JRadioButton("Female", false);
        public GroupPanel1()
        {
            this.setBorder(BorderFactory.createTitledBorder("Panel 1"));
            GroupLayout layout = new GroupLayout(this);
            this.setLayout(layout);
            this.setBackground(Color.white);
            layout.setAutoCreateGaps(true);
            layout.setAutoCreateContainerGaps(true);
            layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addComponent(label1)
                    .addComponent(label2)
                    .addComponent(label3))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(field1)
                    .addComponent(field2)
                    .addComponent(field3))
            );
            layout.setVerticalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(label1)
                    .addComponent(field1))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(label2)
                    .addComponent(field2))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(label3)
                    .addComponent(field3))
            );
            this.setVisible(true);
        }
    }

Panel2:

    package exercise1;

    /**
     *
     * @author Prashanth
     */
    import java.awt.Color;
    import javax.swing.BorderFactory;
    import javax.swing.GroupLayout;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;

    /**
     *
     * @author Prashanth
     */
    public class GroupPanel2 extends JPanel
    {
        private JLabel label1 = new JLabel("Primary:");
        private JTextField field1 = new JTextField(16);
        private JLabel label2 = new JLabel("Secondary:");
        private JTextField field2 = new JTextField(16);
        private JLabel label3 = new JLabel("Tertiary:");
        private JTextField field3 = new JTextField(16);
        private JRadioButton male=new JRadioButton("Male", false);
        private JRadioButton female=new JRadioButton("Female", false);
        public GroupPanel2()
        {
            this.setBorder(BorderFactory.createTitledBorder("Panel 2"));
            GroupLayout layout = new GroupLayout(this);
            this.setLayout(layout);
            this.setBackground(Color.white);
            layout.setAutoCreateGaps(true);
            layout.setAutoCreateContainerGaps(true);
            layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addComponent(label1)
                    .addComponent(label2)
                    .addComponent(label3))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(field1)
                    .addComponent(field2)
                    .addComponent(field3))
            );
            layout.setVerticalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(label1)
                    .addComponent(field1))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(label2)
                    .addComponent(field2))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(label3)
                    .addComponent(field3))
            );
            this.setVisible(true);
        }
    }`

My main class:

    package exercise1;

    import java.awt.Color;
    import java.awt.EventQueue;
    import javax.swing.BorderFactory;
    import javax.swing.GroupLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    /**
     *
     * @author Prashanth
     */
    public class GroupPanelMain extends JPanel
    {
        GroupPanelMain()
        {
            GroupLayout layout = new GroupLayout(this);
            //this.setLayout(layout);
            layout.setAutoCreateGaps(true);
            layout.setAutoCreateContainerGaps(true);
            layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addComponent(new GroupPanel1())
                    .addComponent(new GroupPanel2())
                    .addComponent(new GroupPanel3()))
            );
            layout.setVerticalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(new GroupPanel1()))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(new GroupPanel2()))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(new GroupPanel2()))
            );
        }
        public static void main(String[] args) {
            // TODO code application logic here
            //JPanel panel = new JPanel();
            JFrame frame = new JFrame("Sample");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            /*GroupLayout layout = new GroupLayout(panel);
            panel.setLayout(layout);
            layout.setAutoCreateGaps(true);
            layout.setAutoCreateContainerGaps(true);
            layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addComponent(new GroupPanel1())
                    .addComponent(new GroupPanel2())
                    .addComponent(new GroupPanel3()))
            );
            layout.setVerticalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(new GroupPanel1()))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(new GroupPanel2()))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(new GroupPanel2()))
            );
            frame.add(new GroupPanel1(), GroupLayout.Alignment.LEADING);
            frame.add(new GroupPanel2(), GroupLayout.Alignment.CENTER);
            frame.add(new GroupPanel3(), GroupLayout.Alignment.TRAILING);
            frame.add(new GroupPanelMain());*/
            frame.add(new GroupPanelMain());
            frame.setLocationRelativeTo(null);
            frame.pack();
            frame.setVisible(true);
        }

    }

Please help me solving this problem. Thanks in advance.

Community
  • 1
  • 1
Prashanth
  • 1
  • 4
  • It's not clear what you're trying to do; a sketch might help. Don't neglect to cite the [original code](http://stackoverflow.com/a/8504753/230513), as [required](http://stackexchange.com/legal) by the terms of service. – trashgod Jul 20 '14 at 09:13
  • SO where is the problem? – Braj Jul 20 '14 at 11:19
  • I'am trying to add two JPanels (GroupPanel1 and GroupPanel2) into the main panel (GroupPanelMain). While i'am trying to do this i get GroupPanel's horizontal layout is not included.. First of all is the above code correct implementation. – Prashanth Jul 21 '14 at 10:21

0 Answers0