Solved

Laying out a dialog

Posted on 1998-08-31
7
254 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Timer (static) 9 43
windows explorer path to command prompt 5 44
fibonacci ten numbers 4 39
Java: How do I open the default windows program for a given file type 3 28
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

777 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