How to place components at specific positions?
awt, flowlayout, java, swing
Solution
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();
}
});
}
}
Problem
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.