A function accepting 3 argument : element to be added and the type of it(JLabel or JTexField) and the labels corresponding to which the current label need to be positioned
Expand|Select|Wrap|Line Numbers
- private void setCustomBounds(Object elemnt, String type, JLabel refLabel){
- JLabel lbl;
- JTextField txtField;
- int xField, yField; // variables for x and y Co-ordinate to set bounds , Height and Width will be used from the variable set above
- 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; // need to get the y-axis Co-ordinate for the referenCe Label
- //and then add the height of that Component(beCause the values drawn in java are from top to bottom)
- // and add the gap between two elements
- lbl.setBounds(xField, yField, width, height); // Setting the bounds for the Component
- lbl.setFont(lblFont); // Setting the font as desCribed above in the variable
- add(lbl); //adding the Component to the layout
- //revalidate();
- //repaint();
- }
- //set Bounds(x,y) for TextFiled
- if(type.equalsIgnoreCase("txtBox")){
- txtField=((JTextField)elemnt);
- System.out.println("Setting TextField bounds with reference to beside label");
- xField = xAxis+width+gap; // xAxis + width define the right x-axis Co-ordinate for the referenCe Label
- yField = refLabel.getY(); // as the left side Label will be on the same height so getY() will be enough
- txtField.setBounds(xField, yField, width, height); // Setting the bounds for the Component
- add(txtField); // add the Component to the Layout
- }
- }
The 'setCustomBounds' function is being called from main function :
Expand|Select|Wrap|Line Numbers
- lbl_empid = new JLabel("Employee-ID");
- // lbl_empid.setBounds(xAxis, lbl_title.getHeight() + lbl_title.getY() + gap, width, height);
- //lbl_empid.setFont(lblFont);
- //add(lbl_empid)
- setCustomBounds(lbl_empid, "label", lbl_title);
using above code I am unable to add the label to the display.
However, if the label is added without calling function, it is getting added.