Hi guys,
I have a problem with arranging my components in a GridBagLayout.
The frame is 600x600. Inside it, there are two pairs of (Label,TextField). They are not where I want them to be. What I would like is to have them all at the top of the frame, one after each other. Moreover, I would like all the labels aligned together (easy) but also all the textField aligned together...
Now, my pairs of (Label,TextField) are spread all over the panel, they aren't all at the top. The second pair is at the middle of the frame, and I would like it to be right after the first pair, just following it. And of course, the frame have to stay 600x600. I would like to be able to do that using the GridBagLayout because it will be useful later if I need to do any modification.
I probably did all wrong with the anchors.
Any idea how to get this result ?
Thanks heaps!
import java.awt.*;
import javax.swing.*;
public class GUI_example extends JFrame {
private final static long serialVersionUID = 1L;
JFrame frame;
JLabel labelIndex;
JLabel labelCategory;
JLabel labelUnicode;
JTextField textFieldIndex;
JTextField textFieldCategory;
JTextField textFieldUnicode;
public GUI_example() {
super();
createGUI();
}
public void createGUI() {
frame = new JFrame();
frame.setPreferredSize(new Dimension(600,600));
frame.setTitle("Hello!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = frame.getContentPane();
pane.setLayout(new GridBagLayout());
GridBagConstraints c1 = new GridBagConstraints();
labelIndex = new JLabel("Index: ");
c1.anchor = GridBagConstraints.NORTHWEST;
c1.weightx = 0.5;
c1.weighty = 0.5;
c1.ipady = 10;
c1.gridx = 0;
c1.gridy = 0;
pane.add(labelIndex,c1);
textFieldIndex = new JTextField("Some index...");
c1.gridx = 1;
c1.gridy = 0;
pane.add(textFieldIndex,c1);
labelCategory = new JLabel("Category: ");
c1.anchor = GridBagConstraints.NORTHWEST;
c1.weightx = 0.5;
c1.weighty = 0.5;
c1.gridx = 0;
c1.gridy = 1;
pane.add(labelCategory,c1);
textFieldCategory = new JTextField("Some category...");
c1.gridx = 1;
c1.gridy = 1;
pane.add(textFieldCategory,c1);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new GUI_example();
}
}