I have to create a small form page containing several label and text fields beside that and button to take action on that.
At the top one label has been set which displays some common information. There is one function which set the bounds for all and add them to the default Component.
However, I am unable to see the components in the final result. All the declarations has been done before.
lbl_empid = new JLabel("Employee-ID");
/*lbl_empid.setBounds(xAxis, lbl_title.getHeight() + lbl_title.getY() + gap, width, height);
lbl_empid.setFont(lblFont);*/
setCustomBounds(lbl_empid, "label", lbl_title);
add(lbl_empid);
txt_id = new JTextField();
txt_id.setBounds(lbl_empid.getWidth() + lbl_empid.getX() + gap, lbl_title.getHeight() + lbl_title.getY() +gap, width, height);
add(txt_id);
The setCustomBounds functions is below
private void setCustomBounds(Object elemnt, String type, JLabel refLabel){
JLabel lbl;
JTextField txtField;
int xField, yField;
//System.out.println("Inside set customBounds");
System.out.println("Setting the bounds for "+type);
//set Bounds(x,y) for Label
if (type.equalsIgnoreCase("label")){
lbl=((JLabel)elemnt);
System.out.println("setting label bounds with referen to above label");
xField=xAxis;
yField=refLabel.getY()+height+gap;
lbl.setBounds(xField, yField, width, height);
lbl.setFont(lblFont);
//add(lbl);
}
//set Bounds(x,y) for TextFiled
if(type.equalsIgnoreCase("txtBox")){
txtField=((JTextField)elemnt);
System.out.println("setting Text Box bounds with reference to beside label");
xField = xAxis+width+gap;
yField = refLabel.getY();
txtField.setBounds(xField, yField, width, height);
add(txtField);
}
}
in the above code the Label is not getting displayed. However, the text field is getting displayed.
Second question, can I add the component in the setCustomBounds functions? Third Question, Can I allocate the memory to the components in the setCustomBounds function instead of allocating and passing to the setCustomBounds .