How to place components in certain positions?

How to place components in the layout at a specific position. For example, I want to place 2 text fields on the first line, below 3 combo boxes.

But when I try to put all of them appear on the same line, and I used flowlayout. I also used the border. When I resize, the dimensions of the components window go out of bounds.

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

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.

+6
source share
2 answers

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

You can use a GridLayout with two rows (for example, there are some other possible combinations), each row contains 3 JComboBoxs and two JTextFields respectively.

Look carefully at the documentation and see some examples that are readily available on the Internet.

 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(); } }); } } 
+5
source

The most specific type of layout is absolute positioning.

Warning: Absolute positioning should be used rarely, if ever. There are many reasons for this. Here is one of them: Absolute positioning (without layout manager) and absolute positioning in MiGlayout

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

As said, here's how to use absolute positioning:

In the above code, instead of setting topPanel layout to FlowLayout set it to null .

 topPanel.setLayout(null); 

Later in the code, before loading the components in topPanel call the setBounds container:

 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 the coordinate of roomTypeCombo x relative to the parent, topPanel .
int y-coord - set roomTypeCombo y-coordinate relative to the parent, topPanel .
int width, int height - The last two parameters determine the width and height of roomTypeCombo , which is pretty clear.


I would just play around with the coordinates and see if you like something that comes out of it, the worst thing that can happen is to return to using the layout, which is probably better than absolute positioning. Or you can implement your own layout manager if you follow this hyperlink, the first answer talks about implementing your own layout manager and useful links.

Sorry if none of what I wrote to you helped anyway.

Additional information on absolute positioning

+7
source

Source: https://habr.com/ru/post/912409/


All Articles