Line up columns

Posted on 2004-04-02
Last Modified: 2010-03-31
I am trying to get this code to line up in 2 columns with the first 2 labels as headers, am i going the right way about it, trying to teach myself Java, having difficulty?  Here is the code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
class AddMember1 extends JFrame{
      String dt[] = {"Antrim","Armagh","Carlow","Cavan","Clare","Cork","Derry","Donegal","Down","Dublin","Fermanagh","Galway","Kerry","Kildare","Kilkenny","Laois","Leitrim","Limerick","Longford","Louth","Mayo","Meath","Monaghan","Offaly","Roscommon","Sligo","Tipperary","Tyrone","Waterford","Westmeath","Wexford","Wicklow"};
      String fm[] = {"Male","Female"};            
      String ai[] = {"Active","Inactive"};      
      //set up row 1
      JPanel row1 = new JPanel();
      JLabel techno = new JLabel("TECHNOFIT GYM SYSTEM", SwingConstants.CENTER );

      //set up row 2
      JPanel row2 = new JPanel();
      JLabel add = new JLabel("Add Member", SwingConstants.CENTER );
      //set up row 3
      JPanel row3 = new JPanel();
      JLabel memberid = new JLabel("Member ID");
      JTextField memberidvalue = new JTextField(10);
      //set up row 4
      JPanel row4 = new JPanel();      
      JLabel forename = new JLabel("Forename");
      JTextField forenamevalue = new JTextField(10);
      //set up row 5
      JPanel row5 = new JPanel();
      JLabel surname = new JLabel("Surname");
      JTextField surnamevalue = new JTextField(10);
      //set up row 6
      JPanel row6 = new JPanel();      
      JLabel address1 = new JLabel("Address 1");
      JTextField address1value = new JTextField(10);
      //set up row 7
      JPanel row7 = new JPanel();
      JLabel address2 = new JLabel("Address 2");
      JTextField address2value = new JTextField(10);
      //set up row 8
      JPanel row8 = new JPanel();
      JLabel county = new JLabel("County");
      JComboBox combo;
      //set up row 9
      JPanel row9 = new JPanel();
      JLabel height = new JLabel("Height");
      JTextField heightvalue = new JTextField(10);
      //set up row 10
      JPanel row10 = new JPanel();      
      JLabel teleno = new JLabel("Telephone No");
      JTextField telenovalue = new JTextField(10);      
      //set up row 11
      JPanel row11 = new JPanel();      
      JLabel dob = new JLabel("DOB");
      JTextField dobvalue = new JTextField(10);
      //set up row 12
      JPanel row12 = new JPanel();            
      JLabel gender = new JLabel("Gender");
      JComboBox combo1;

      //set up row 13
      JPanel row13 = new JPanel();
      JLabel dateadded = new JLabel("Date Added");
      JTextField dateaddedvalue = new JTextField(10);
      //set up row 14
      JPanel row14 = new JPanel();
      JLabel expirydate = new JLabel("Expiry Date");
      JTextField expirydatevalue = new JTextField(10);      
      //set up row 15
      JPanel row15 = new JPanel();      
      JLabel status = new JLabel("Status");
      JComboBox combo2;
      //set up row 16
      JPanel row16 = new JPanel();
      private JButton b1;

public AddMember1()
            super("Add Member");
            setSize(500, 550);
            GridLayout layout = new GridLayout(8,2);
            b1 = new JButton("OK");
            Container c = getContentPane();
            c.setLayout(new FlowLayout());
            FlowLayout layout1 = new FlowLayout(FlowLayout.LEFT, 10, 10 );
            FlowLayout layout2 = new FlowLayout(FlowLayout.LEFT, 10, 10 );

            FlowLayout layout3 = new FlowLayout(FlowLayout.LEFT);

            FlowLayout layout4 = new FlowLayout(FlowLayout.LEFT);

            FlowLayout layout5 = new FlowLayout(FlowLayout.LEFT);

            FlowLayout layout6 = new FlowLayout(FlowLayout.LEFT);

            FlowLayout layout7 = new FlowLayout(FlowLayout.LEFT);

            FlowLayout layout8 = new FlowLayout(FlowLayout.LEFT);
            combo = new JComboBox(dt);

            FlowLayout layout9 = new FlowLayout(FlowLayout.LEFT);

            FlowLayout layout10 = new FlowLayout(FlowLayout.LEFT);

            FlowLayout layout11 = new FlowLayout(FlowLayout.LEFT);

            FlowLayout layout12 = new FlowLayout(FlowLayout.LEFT);
            combo1 = new JComboBox(fm);

            FlowLayout layout13 = new FlowLayout(FlowLayout.LEFT);

            FlowLayout layout14 = new FlowLayout(FlowLayout.LEFT);

            FlowLayout layout15 = new FlowLayout(FlowLayout.LEFT);
            combo2 = new JComboBox(ai);

            FlowLayout layout16 = new FlowLayout(FlowLayout.LEFT);
            //add button actionlistener
            b1.addActionListener(new ButtonListenerB1());

            addWindowListener(new WindowAdapter(){
                  public void windowClosing(WindowEvent e){


      class ButtonListenerB1 implements ActionListener
            public void actionPerformed(ActionEvent e)

      public static void main(String[] args)
            AddMember1 memb = new AddMember1();
Question by:powerger
  • 2
LVL 14

Expert Comment

ID: 10739301
First change your layout from default FlowLayout to some other more appropriate like BorederLayout. Also by doing this, you should consider using several JPane's each for next column or something like that
LVL 14

Accepted Solution

StillUnAware earned 63 total points
ID: 10739410
public AddMember1()
          super("Add Member");
          setSize(500, 550);
          GridLayout layout = new GridLayout(8,2);
          b1 = new JButton("OK");
          Container c = getContentPane();
          c.setLayout(new FlowLayout()); <<<<<<<<<<<<< you should change to :      c.setLayout(layout);
LVL 30

Assisted Solution

GrandSchtroumpf earned 62 total points
ID: 10739884
You should use a JPanel with GridLayout and add all your components to that JPanel.
Then you add the JPanel to your content pane using FlowLayout.
Finally, you change all the "FlowLayout.LEFT" to "FlowLayout.RIGHT".
Then set the preferred size of your combo box to the same preferred size as the text fields, so your components align nicely.

public AddMember1()
          super("Add Member");
          setSize(500, 550);
          GridLayout layout = new GridLayout(8,2);
          b1 = new JButton("OK");
          Container c = new JPanel(layout);
          getContentPane().setLayout(new FlowLayout());
          FlowLayout layout1 = new FlowLayout(FlowLayout.RIGHT, 10, 10 );
          FlowLayout layout2 = new FlowLayout(FlowLayout.RIGHT, 10, 10 );
          // This needs to be placed at the end of the method

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 214
tomcat administrtor 12 61
hibernate example issues from command prompt 10 50
Java class and jar 3 25
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question