Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place components at specific positions?

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.

like image 954
Ravi Avatar asked Apr 04 '12 16:04

Ravi


People also ask

Which is used to place components in a specific manner?

The LayoutManagers are used to arrange components in a particular manner.

Which layout organizes components in five regions?

The borderlayout arranges the components to fit in the five regions: east, west, north, south and center.


2 Answers

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

like image 195
Jonny Henly Avatar answered Nov 09 '22 21:11

Jonny Henly


Try to change the layout. http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

You could go for a GridLayout with two lines (for example, there is some others possible combinations), with each line containing respectively 3 JComboBoxs, and two JTextFields.

Look carefully at the documentation and check out some examples easily reachable on the web.

import java.awt.GridLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SwingResizeJFrame {

    public SwingResizeJFrame() {
        JTextField TextField1 = new JTextField("firstTextField");
        JTextField TextField2 = new JTextField("secondTextField");
        JPanel firstPanel = new JPanel();
        firstPanel.setLayout(new GridLayout(0, 2, 10, 10));
        firstPanel.add(TextField1);
        firstPanel.add(TextField2);

        JComboBox comboBox1 = new JComboBox(new Object[]{"Ester", "Jordi", "Jordina", "Jorge", "Sergi"});
        JComboBox comboBox2 = new JComboBox(new Object[]{"Ester", "Jordi", "Jordina", "Jorge", "Sergi"});
        JComboBox comboBox3 = new JComboBox(new Object[]{"Ester", "Jordi", "Jordina", "Jorge", "Sergi"});
        JPanel secondPanel = new JPanel();
        secondPanel.setLayout(new GridLayout(0, 3, 10, 10));
        secondPanel.add(comboBox1);
        secondPanel.add(comboBox2);
        secondPanel.add(comboBox3);

        JFrame frame = new JFrame();
        frame.setLayout(new GridLayout(2, 1, 10, 10));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(firstPanel);
        frame.add(secondPanel);
        frame.pack();
        frame.setLocation(150, 150);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                SwingResizeJFrame demo = new SwingResizeJFrame();
            }
        });
    }
}
like image 41
Jeremy D Avatar answered Nov 09 '22 19:11

Jeremy D