Solved

Laying out a dialog

Posted on 1998-08-31
7
253 Views
Last Modified: 2010-03-30
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!)
0
Comment
Question by:dirku
  • 3
  • 3
7 Comments
 
LVL 2

Expert Comment

by:shchuka
ID: 1223044
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.
0
 

Author Comment

by:dirku
ID: 1223045
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.
0
 

Accepted Solution

by:
tgoglia earned 30 total points
ID: 1223046
You can get the current width and height of the window in question with the following code:

Dimension winSize=size();
windowWidth=winSize.width();
windowHeight=winSize.height();

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:

myButton.move((windowWidth/2)-100,yValue)

will center the button.
tgoglia

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:tgoglia
ID: 1223047
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.

0
 

Expert Comment

by:tgoglia
ID: 1223048
  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
0
 

Author Comment

by:dirku
ID: 1223049
I will try this out soon!
0
 

Author Comment

by:dirku
ID: 1223050
I will try this soon!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now