0

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 .

Learner
  • 1,544
  • 8
  • 29
  • 55
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). .. – Andrew Thompson Feb 01 '15 at 21:15
  • 1
    .. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). Note that a text filed will be sized according to font and the number of characters (columns) it is set to display. – Andrew Thompson Feb 01 '15 at 21:16
  • 1
    *"Second question,.."* SO is not a smorgasbord or a help desk, but a Q&A site. Each thread should have ***one*** clear, specific question. If you have more than one, start more threads. – Andrew Thompson Feb 01 '15 at 21:17
  • @AndrewThompson: Thanks for Improving the public pages. Could you please give an example of MCVE using code in my question. – Learner Feb 01 '15 at 21:31
  • *"Could you please give an example of MCVE.."* I possibly could, but won't attempt to do so on the basis that I might fix something in code not shown, that your actual code breaks. What is it you do not understand about making an MCVE? – Andrew Thompson Feb 01 '15 at 21:52
  • @AndrewThompson: As per my understanding , I have already given the minimum code . – Learner Feb 01 '15 at 23:13
  • 1
    Is it your attention span that is minimal? There are 4 parts to being an MCVE, and so far you've managed to understand the 1st part. – Andrew Thompson Feb 01 '15 at 23:19

0 Answers0