Solved

Laying out a dialog

Posted on 1998-08-31
7
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

732 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