Laying out a dialog

I have some trouble with laying out a dialog's components.
Can anybody provide me with some source code?

Presently I try to layout the dialog with a BoxLayout. It should contain 5 rows. The rows should have a label describing the component on the right side of this label.
The first row should have a label and then a JComboBox should follow.
The following 4 rows should have a label and the a JTextField should follow.

The problem: The labels aren't at the same y-coordinate as the components right beneath!

Maybe there is a better LayoutManager to use. As I said before I use a BoxLayout. I have made a JPanel for the Labels and a JPanel for the components described by the JLabels. These JPanels use a GridLayout and then I have layed out these JPanels within the Boxes.

The Components which should be in the Dialog:
1. An Image and a text(JLabel) describing the use of the Dialog (both in one row).
2. A JSeparator
3. A JLabel and a JComboBox (both in one row)
4. 5 JLabels in one column and for each JLabel one JTextField right beside the JLabel. Only in the first row of these five a JComboBox should be rather than a JTextField!
The two last rows holding a JLabel and a JTextField each should be followed by another JLabel.
The JComboBox as well as the JTextFields should appaer at the same x-coordinate and they should not take the space left to the right side of the JDialog!
5. A JSeparator
6. Two JButtons (OK and Cancel)

I hope anyone can provide me with some source code to reach what I want. Maybe the BoxLayout shouldn't be taken in this case? (Of course, I don't want absolute coordinates for the components, it should be scalable!)
Who is Participating?
tgogliaConnect With a Mentor Commented:
You can get the current width and height of the window in question with the following code:

Dimension winSize=size();

Then you can set the x and y coordinates positioning your components using this information along with the size of your widget. Assuming the button in question is 100 pixels wide:


will center the button.

I have been having all sorts of trouble using layout managers when positioning controls exactly.  The solution I came to is the following:


jlb1 = new JLabel("Label 1");
jtf1 = new JTextField();




This way you can specify exactly (in pixels) what the size of each component is and where on the dialog they are located.

Good luck.
dirkuAuthor Commented:
As I mentioned, I don't want to use absolute positions.
When changing the resolution say from 800x600 to 640x480 the components will not be layed out properly.
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

I was in a rush to get to lunch and wrote down the math wrong.
To center the button with a width of 100 pixels,you need the following code:

Also: a reminder to still set the layout to null.

  Here is some quick code that I threw together. It is NOT compilable, but gives the basic idea. You will probably need to play around with resizing the panels to get it to look the way that you would like- creating other panels for the components I didn't mention( though I would probably handle the first row including the Image and the directions in the paint method), and will need to decide how to center the panels on the y-axis.
public void init(){
      //find window dimensions
      Dimension winSize=size();
      int winWidth=winSize.width();
      int winHeight=winSize.height();

      //Set first row
      JPanel firstRow=new JPanel();
      firstRow.setLayout(new GridLayout(2,1));
            jlb1=new JLabel("Label 1");
            myComboBox=new JComboBox();
                  //add combo box items
      //Resize and show panel

      //Widget rows
      JPanel widgetPanel=new JPanel();
      widgetPanel.setLayout(new GridLayout(2,5));
            //initiate labels
            jlb1=new JLabel("Label 1");
            jlb2=new JLabel("Label 2")
            jlb3=new JLabel("Label 3");
            //initiate combobox
            myComboBox=new JComboBox();
                  //add combo box items
            //initiate TextFields
            text1=new JTextField();
            text2=new JTextField();
      //Add components widgetPanel

      //Resize and show widgetPanel

      //Create widgetPanel2
      widgetPanel2=new JPanel();
      widgetPanel2.setLayout(new GridLayout(2,3);

            //initiate labels and TextFields
            jlb4=new JLabel("Label 4");
            jlb5=new JLabel("Label 5");
            jlb6=new JLabel("Label 6");
            jlb6=new JLabel("Label 7");
            text3=new JTextField();
            text4=new JTextField();



      //Resize and show widgetPanel2

      //Create buttonPanel
      JPanel buttonPanel=new JPanel();
            enterBtn=new JButton("Enter")
            cancelBtn=new JButton("Cancel")

            //add buttons

      //Resize Button Panel
Good Luck and I hope this is helpful,
dirkuAuthor Commented:
I will try this out soon!
dirkuAuthor Commented:
I will try this soon!
All Courses

From novice to tech pro — start learning today.