I need to align all the fields with the respective labels,
here is my
The only reliable way to get the labels and their corresponding fields to line up properly is to put them in the same panel and use a single instance of a layout manager to line them up.
GridLayout
The simplest approach would be to use a two-column Grid layout:
JPanel fieldPane = new JPanel(new GridLayout(0, 2));
fieldPane.add(ClientIP);
fieldPane.add(ip);
fieldPane.add(ClientPassword);
fieldPane.add(pass);
fieldPane.add(Videoname);
fieldPane.add(vname);
fieldPane.add(perccomplete);
fieldPane.add(percent);
fieldPane.add(PacketsSent);
fieldPane.add(pacsent);
fieldPane.add(Connectiontype);
fieldPane.add(conntype);
fieldPane.add(noofvideossent);
fieldPane.add(videosend);
This has the one disadvantage that all the cells will be the same size. Specifically, all the label cells will be the same width as the field cells. This probably won't matter in this specific case, however in the more general case you would want to use a different layout that allows the first column to remain narrow and gives all the remaining width to the second column.
GridBagLayout
Here's how I'd do that with GridBagLayout:
JPanel fieldPane = new JPanel(new GridBagLayout());
Insets insets = new Insets(2, 2, 2, 2);
GridBagConstraints label = new GridBagConstraints(
0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.BASELINE_LEADING, GridBagConstraints.NONE,
insets, 2, 2);
GridBagConstraints value = new GridBagConstraints(
1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.BASELINE_LEADING, GridBagConstraints.HORIZONTAL,
insets, 2, 2);
fieldPane.add(ClientIP, label);
fieldPane.add(ip, value);
label.gridy++;
value.gridy++;
fieldPane.add(ClientPassword, label);
fieldPane.add(pass, value);
label.gridy++;
value.gridy++;
fieldPane.add(Videoname, label);
fieldPane.add(vname, value);
label.gridy++;
value.gridy++;
fieldPane.add(perccomplete, label);
fieldPane.add(percent, value);
label.gridy++;
value.gridy++;
fieldPane.add(PacketsSent, label);
fieldPane.add(pacsent, value);
label.gridy++;
value.gridy++;
fieldPane.add(Connectiontype, label);
fieldPane.add(conntype, value);
label.gridy++;
value.gridy++;
fieldPane.add(noofvideossent, label);
fieldPane.add(videosend, value);
Because I use GridBagLayout a lot, I subclassed GridBagConstraints to make my life a little easier. My subclass has, amongst others, the following method:
public SuperGBC at(int x, int y) {
gridx = x;
gridy = y;
return this;
}
It has similar methods anchor()
, fill()
and insets()
for initialising itself without having to specify all values. This makes the above code somewhat more compact:
JPanel fieldPane = new JPanel(new GridBagLayout());
Insets insets = new Insets(2, 2, 2, 2);
SuperGBC label = new SuperGBC()
.anchor(SuperGBC.BASELINE_LEADING)
.insets(insets);
SuperGBC value = new SuperGBC()
.anchor(SuperGBC.BASELINE_LEADING)
.insets(insets)
.fill(GridBagConstraints.HORIZONTAL, 0.0, 0.0);
fieldPane.add(ClientIP, label.at(0, 0));
fieldPane.add(ip, value.at(1, 0));
fieldPane.add(ClientPassword, label.at(0, 1));
fieldPane.add(pass, value.at(1, 1));
fieldPane.add(Videoname, label.at(0, 2));
fieldPane.add(vname, value.at(1, 2));
fieldPane.add(perccomplete, label.at(0, 3));
fieldPane.add(percent, value.at(1, 3));
fieldPane.add(PacketsSent, label.at(0, 4));
fieldPane.add(pacsent, value.at(1, 4));
fieldPane.add(Connectiontype, label.at(0, 5));
fieldPane.add(conntype, value.at(1, 5));
fieldPane.add(noofvideossent, label.at(0, 6));
fieldPane.add(videosend, value.at(1, 6));