1 Attachment(s)
How to set layout to have correct output
I have to create a frame with tabs. So I have lots of labels and fields.
I tried many layouts(like Grid, GridBag, Boarder, Flow) for setting them in different tabs. But I couldn't get satisfactory results.
Below is the code for one of the tab :-
Code:
class SCGUI extends JPanel {
public SCGUI() {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
JPanel shortRightPanel = new JPanel();
JPanel cboxPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
shortRightPanel.setLayout(new BoxLayout(shortRightPanel, BoxLayout.Y_AXIS));
cboxPanel.setLayout(new BoxLayout(cboxPanel, BoxLayout.Y_AXIS));
JLabel shortCodeLabel = new JLabel("Short Code : - ");
JLabel shortCodeOwnerLabel = new JLabel("Short Code Owner : - ");
JLabel connectionTypeLabel = new JLabel("Connection Type : - ");
JTextField shortCodeTextField = new JTextField(10);
shortCodeTextField.setSize(100, 30);
JComboBox comboboxConnectionType = new JComboBox();
JTextField shortCodeOwnerTextField = new JTextField(50);
shortCodeOwnerTextField.setSize(200, 30);
leftPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
leftPanel.add(Box.createRigidArea(new Dimension(5, 35)));
leftPanel.add(shortCodeLabel);
leftPanel.add(Box.createRigidArea(new Dimension(5, 25)));
leftPanel.add(shortCodeOwnerLabel);
leftPanel.add(Box.createRigidArea(new Dimension(5, 25)));
leftPanel.add(connectionTypeLabel);
shortRightPanel.setMinimumSize(new Dimension(100,20));
shortRightPanel.setMaximumSize(new Dimension(100,20));
shortRightPanel.setPreferredSize(new Dimension(100,20));
cboxPanel.setMinimumSize(new Dimension(100,30));
cboxPanel.setMaximumSize(new Dimension(100,30));
cboxPanel.setPreferredSize(new Dimension(100,30));
shortCodeTextField.setAlignmentX(Component.LEFT_ALIGNMENT);
shortRightPanel.add(shortCodeTextField);
cboxPanel.add(comboboxConnectionType);
rightPanel.add(Box.createRigidArea(new Dimension(5, 40)));
rightPanel.add(shortRightPanel);
rightPanel.add(Box.createRigidArea(new Dimension(5,20)));
rightPanel.add(shortCodeOwnerTextField);
rightPanel.add(Box.createRigidArea(new Dimension(5,10)));
rightPanel.add(cboxPanel);
mainPanel.add(leftPanel);
mainPanel.add(rightPanel);
add(mainPanel);
}
}
and Output looks like : -
How can I make it look correct.