By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,045 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,045 IT Pros & Developers. It's quick & easy.

Adding JLabel Component to flow layout by calling custom function is not working

P: 98
I am adding multiple labels and text boxes in one form display using swing. Because the height width and the inter-spaces are consistent so I am using common function to add Labels and Text box.

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
  1.  
  2.          private void setCustomBounds(Object elemnt, String type, JLabel refLabel){
  3.         JLabel lbl;
  4.         JTextField txtField;
  5.         int xField, yField; // variables for x and y Co-ordinate to set bounds , Height and Width will be used from the variable set above
  6.  
  7.         System.out.println("Setting the bounds for "+type);
  8.  
  9.         //set Bounds(x,y) for Label
  10.         if (type.equalsIgnoreCase("label")){
  11.             lbl=((JLabel)elemnt);
  12.  
  13.             System.out.println("Setting label bounds with referen to above label");
  14.             xField=xAxis;
  15.             yField=refLabel.getY()+height+gap;  // need to get the y-axis Co-ordinate for the referenCe Label 
  16.                                             //and then add the height of that Component(beCause the values drawn in java are from top to bottom) 
  17.                                             // and add the gap between two elements
  18.                lbl.setBounds(xField, yField, width, height); // Setting the bounds for the Component
  19.                lbl.setFont(lblFont); // Setting the font as desCribed above in the variable
  20.                add(lbl);        //adding the Component to the layout
  21.                //revalidate();
  22.             //repaint();
  23.  
  24.         }
  25.  
  26.         //set Bounds(x,y) for TextFiled
  27.         if(type.equalsIgnoreCase("txtBox")){
  28.             txtField=((JTextField)elemnt);
  29.  
  30.             System.out.println("Setting TextField bounds with reference to beside label");
  31.             xField = xAxis+width+gap;       // xAxis + width define the right x-axis Co-ordinate for the referenCe Label
  32.             yField = refLabel.getY();       // as the left side Label will be on the same height so getY() will be enough
  33.             txtField.setBounds(xField, yField, width, height);   // Setting the bounds for the Component
  34.             add(txtField);   // add the Component to the Layout
  35.  
  36.         }        
  37.  
  38.  
  39.     }
  40.  
xAxis,yAxis,gap,height and width variable values has been defined privately

The 'setCustomBounds' function is being called from main function :

Expand|Select|Wrap|Line Numbers
  1. lbl_empid = new JLabel("Employee-ID");
  2.        // lbl_empid.setBounds(xAxis, lbl_title.getHeight() + lbl_title.getY() + gap, width, height);
  3.         //lbl_empid.setFont(lblFont);
  4.         //add(lbl_empid)
  5.  
  6.          setCustomBounds(lbl_empid, "label", lbl_title);
  7.  
  8.  
lbl_title is a label positioned at the top for which bounds has been defined already .

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.
Feb 7 '15 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.