tags:

views:

45

answers:

2

I have a grid layout, consisting of 4 columns. Just one row. Inside each grid layout box i have a box layout so i can stack things on top of each other.

The problem is, i have 5 things in column one, 4 in column 2, 3 and 4 are irellevant for now. The issue is i want the items in columns 1 and 2 to match, but because the 2nd has 4 items in it, its stretching those to fit the size of column 1 which has 5 things inside it

Ok its complicated to explain, here is the code i am working on

import javax.swing.*;
import java.awt.*;

public class myGui extends JFrame
{
      /**
       * Constructor for my GUI class
       */
      public myGui()
      {
      }

      /**
       * @param args
       */
      public static void main(String[] args)
      {
            createFrame();
      }

      /**
       * Build the gui
       */
    public static void createFrame ()
    {
        JFrame frame = new JFrame("This is my gui");
        JPanel content = new JPanel(new GridLayout(1, 4));
        frame.getContentPane().add(content);
        content.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Requestor"));
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

       //Create a box grid layout inside a panel and apply this to the grid view colums
        JPanel col1 = new JPanel();
        BoxLayout boxLayout1 = new BoxLayout(col1, BoxLayout.Y_AXIS);
        col1.setLayout(boxLayout1);

        JPanel col2 = new JPanel();
        BoxLayout boxLayout2 = new BoxLayout(col2, BoxLayout.Y_AXIS);
        col2.setLayout(boxLayout2);

        JPanel col3 = new JPanel();
        BoxLayout boxLayout3 = new BoxLayout(col3, BoxLayout.Y_AXIS);
        col3.setLayout(boxLayout3);

        JPanel col4 = new JPanel();
        BoxLayout boxLayout4 = new BoxLayout(col4, BoxLayout.Y_AXIS);
        col4.setLayout(boxLayout4);

        //Add the boxLayouts to the appropriate columns
        content.add(col1);
        content.add(col2);
        content.add(col3);
        content.add(col4);   

        //create JLabels
        JLabel requestorId = new JLabel("RequestorID");
        JLabel org = new JLabel("Organisation");
        JLabel orgAddress = new JLabel("Organisation Address");
        JLabel otherAdd = new JLabel("Other Address");
        JLabel title = new JLabel("Title");
        JLabel phoneNo = new JLabel("Phone Number");
        JLabel firstName = new JLabel("First Name");
        JLabel surname = new JLabel("Surname");
        JLabel emailAdd = new JLabel("E Mail Address");
        JLabel dept = new JLabel("Department");
        JLabel fax = new JLabel("Fax Number");

        //create text areas
        JTextArea reqIDTxt = new JTextArea();
        reqIDTxt.setEditable(false);     

        JTextArea orgTxt = new JTextArea();
        JTextArea orgAddTxt = new JTextArea();
        JTextArea otherAddTxt = new JTextArea();
        JTextArea titleTxt = new JTextArea();
        JTextArea phoneNoTxt = new JTextArea();
        JTextArea firstNameTxt = new JTextArea();
        JTextArea faxNoTxt = new JTextArea();
        JTextArea surnameTxt = new JTextArea();
        JTextArea emailTxt = new JTextArea();
        JTextArea deptTxt = new JTextArea();

        //Add text areas and labels to their respective grid locations
        col1.add(requestorId);
        col1.add(reqIDTxt);
        col1.add(title);
        col1.add(titleTxt);
        col1.add(firstName);
        col1.add(firstNameTxt);
        col1.add(surname);
        col1.add(surnameTxt);
        col1.add(dept);
        col1.add(deptTxt);

        col2.add(org);
        col2.add(orgTxt);
        col2.add(phoneNo);
        col2.add(phoneNoTxt);
        col2.add(fax);
        col2.add(faxNoTxt);
        col2.add(emailAdd);
        col2.add(emailTxt);

        col3.add(orgAddress);
        col3.add(orgAddTxt);

        col4.add(otherAdd);
        col4.add(otherAddTxt);
        frame.pack();
        frame.show();      

    }
}

any ideas how to make the colums line up :/

Thanks

A: 

In this case you should only use the GridLayout and not the box layouts.

In a grid everything will be aligned.

Colin Hebert
i originally tried that but then i had issues with columns 3 and 4 where the text areas were not the size i wanted
rob
In that case take a look at the GridBagLayout http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
Colin Hebert
After a while, I just stopped using layouts besides gridbag and spring. I've found that the simpler layouts such as box and grid will tend to be too restrictive for anything but the most basic layouts, which it sounds like you are moving beyond.
Zoe Gagnon
A: 

You need to give the Layout Manager more information to work with. In this case the BoxLayout respects the maximum size of a component so you need code something like:

JTextArea reqIDTxt = new JTextArea(2, 10);
JTextArea orgTxt = new JTextArea(2, 20);
JTextArea orgAddTxt = new JTextArea(2, 20);
JTextArea otherAddTxt = new JTextArea(2, 20);
JTextArea titleTxt = new JTextArea(2, 20);
JTextArea phoneNoTxt = new JTextArea(2, 20);
JTextArea firstNameTxt = new JTextArea(2, 20);
JTextArea faxNoTxt = new JTextArea(2, 20);
JTextArea surnameTxt = new JTextArea(2, 20);
JTextArea emailTxt = new JTextArea(2, 20);
JTextArea deptTxt = new JTextArea(2, 20);

orgTxt.setMaximumSize( orgTxt.getPreferredSize() );
deptTxt.setMaximumSize( deptTxt.getPreferredSize() );
reqIDTxt.setMaximumSize( reqIDTxt.getPreferredSize() );
titleTxt.setMaximumSize( titleTxt.getPreferredSize() );
firstNameTxt.setMaximumSize( firstNameTxt.getPreferredSize() );
surnameTxt.setMaximumSize( surnameTxt.getPreferredSize() );
orgTxt.setMaximumSize( orgTxt.getPreferredSize() );
phoneNoTxt.setMaximumSize( phoneNoTxt.getPreferredSize() );
faxNoTxt.setMaximumSize( faxNoTxt.getPreferredSize() );
emailTxt.setMaximumSize( emailTxt.getPreferredSize() );
camickr