Question

I am new to gui in java, i spent 3 hours trying to figure out what i have done wrong or misunderstood, i should get this:

enter image description here

but the text in my code is displayed after textfields

   textPanel = new JPanel();
     textPanel.setLayout(new GridLayout(3,0)); 

     fName = new JTextField( 15 ) ; 
     textPanel.add(fName);
     jlbName = new JLabel ( "Firstname" );
    jlbName.setHorizontalAlignment(JLabel.RIGHT);
    textPanel.add(jlbName);

     lName = new JTextField( 15 ) ; 
    textPanel.add(lName);
    jlbName = new JLabel ( "LastName" );

    jlbName.setHorizontalAlignment(JLabel.RIGHT);
    textPanel.add(jlbName);



    libNo = new JTextField( 15 ) ; 
    textPanel.add(libNo);
       libNo.setEditable(false);
    jlbName = new JLabel ( "Library Number" );

    jlbName.setHorizontalAlignment(JLabel.RIGHT);
    textPanel.add(jlbName);


   add(textPanel,BorderLayout.EAST);
   JButton jbtN = new JButton("Add borrower");
   add(jbtN ,BorderLayout.SOUTH);
Was it helpful?

Solution

You are inserting the components into the panel in the wrong order. You first insert the text fields and later the labels. Do the opposite, i.e. instead of:

textPanel.add(fName);
...
textPanel.add(jlbName);
...

do:

textPanel.add(jlbName);
...
textPanel.add(fName);
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top