How to place components at specific positions?

Ravi picture Ravi · Apr 4, 2012 · Viewed 25.8k times · Source

How to place components in layout on specific position. Like I want to place 2 text boxes in first row, below 3 combo boxes.

But when I am trying to put they all appear in one line and I have used flowlayout. I have used the border as well. When I am resizing, the window sizes of the components are going out from border.

Can you suggest me some layouts to use and how to use it?

Here is my code :

topPanel=new JPanel();
topPanel.setLayout(new FlowLayout());
topPanel.setBorder(new TitledBorder(new EtchedBorder(), "Customer Data"));

CNameTextField = new JTextField (20); // create the Customer Name text field
CNameTextField.setEditable(true);     // set editable text box

CIDLabel=new JLabel("Customer ID");

C_IDTextField = new JTextField (10);
C_IDTextField.setEditable(true);      // set editable text box

topPanel.add(CNameTextField);
topPanel.add(C_IDTextField);   

// Create and populate Room type combo box
roomTypeCombo = new JComboBox();
roomTypeCombo.addItem( "Budget($50)" );    

// Create and populate Meal type combo box
mealCombo = new JComboBox();
mealCombo.addItem( "None" );       

// Create and populate Days combo box

daysCombo = new JComboBox();

for(int i=0;i<31 ; i++) {
            // populate combobox with days
    daysCombo.addItem(i); 
}
    // Adding  rest of the components to top panel
topPanel.add(roomTypeCombo);
topPanel.add(mealCombo);
topPanel.add(daysCombo);

Thanks.

Answer

Jonny Henly picture Jonny Henly · Apr 4, 2012

The most specific type of layout is absolute positioning.

Warning: Absolute positioning should rarely, if ever, be used. There are many reasons why. Here is one: Absolute positioning (No layout manager) vs. absolute positioning in MiGlayout

- Thanks to user brimborium for the good idea of adding a warning.

That being said, here is how to use absolute positioning:

In your code above, instead of setting topPanel's layout to FlowLayout, set it to null.

topPanel.setLayout(null);

Later on in the code, right before you start adding components to topPanel, call the container's setBounds method:

someJComponent.setBounds(x-coord, y-coord, width, height);

So for example you created an instance of JComboBox() and named it roomTypeCombo, the following code shows how to absolutely position roomTypeCombo.

topPanel.setLayout(null);

// code...

roomTypeCombo = new JComboBox();

// code...

roomTypeCombo.setBounds(100, 100, 200, 50);
topPanel.add(roomTypeCombo);

The setBounds method, used above, has four parameters:

  • int x-coord - set roomTypeCombo's x-coordinate relative to its parent, topPanel.
  • int y-coord - set roomTypeCombo's y-coordinate relative to its parent, topPanel.
  • int width - specify roomTypeCombo's width.
  • int height - specify roomTypeCombo's height.


I would just play around with the coordinates and see if you like anything that comes out of it. The worst thing that could happen is that you go back to using a layout, which is probably better than absolute positioning. Or you could implement your own layout manager, if you follow this hyperlink the first answer talks about implementing your own layout manager and has helpful links.

More information on absolute positioning