Link to home
Start Free TrialLog in
Avatar of dirku
dirkuFlag for Germany

asked on

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!)
Avatar of shchuka
shchuka

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

myDialog.setLayout(null);

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

jlb1.setSize(80,20);
jtf1.setSize(80,20);

jlb1.setLocation(10,20);
jtf1.setLocation(100,20);

myDialog.add(jlb1,null);
myDialog.add(jtf1,null);

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.
Avatar of dirku

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of tgoglia
tgoglia

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:
myButton.move((windowWidth-100/2),yValue)

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.
CODE:
public void init(){
      setLayout(null);
      //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
      firstRow.add(jlb1);
      firstRow.add(myComboBox);
      
      //Resize and show panel
      firstRow.resize(x,y);
      firstRow.move();
      add(firstRow);
      firstRow.show();

      //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
      widgetPanel.add(jlb1);
      widgetPanel.add(myComboBox);
      widgetPanel.add(jlb2);
      widgetPanel.add(text1);
      widgetPanel.add(jlb3);
      widgetPanel.add(text2);
      

      
      //Resize and show widgetPanel
      widgetPanel.resize(x,y);
      widgetPanelmove((winWidth-x)/2,y);
      add(widgetPanel);
      widgetPanel.show();

      //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();

            widgetPanel2.add(jlb4);
            widgetPanel2.add(text3);
            widgetPanel2.add(jlb5);

            widgetPanel2.add(jlb6);
            widgetPanel2.add(text4);
            widgetPanel2.add(jlb7);

      //Resize and show widgetPanel2
      widgetPanel2.resize(x,y);
      widgetPanel2.move((winWidth-x)/2,y);
      add(widgetPanel2);
      widgetPanel2.show();

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

            //add buttons
            buttonPanel.add(enterBtn);
            buttonPanel.add(cancelBtn);

      //Resize Button Panel
      buttonPanel.resize(x,y);
      buttonPanel.move((winWidth-x)/2,y);
      add(buttonPanel);
      buttonPanel.show();
}
Good Luck and I hope this is helpful,
tgoglia
Avatar of dirku

ASKER

I will try this out soon!
Avatar of dirku

ASKER

I will try this soon!