Hi, was wondering if anyone could help me. I'm creating my own little simple tree structure, consisting of several JTextFields. Each TextField is added to a JPanel, which is finally added to a JScrollPane, which is then added to a JFrame. Everything is fine, apart from the position of the JTextFields. Within the for loop below, I try to position them (so one is below the other), but when I run the program it puts them side by side - no matter what I do in that for loop, I cannot change the position of those TextFields within that JPanel - can someone help me?

public JScrollPane drawTree(){
		int pos = 20;
		JScrollPane scroll = new JScrollPane();
		scroll.setBounds(584, 71, 600, 600);
		//scroll.setLayout(null);
		JPanel p = new JPanel();
		
		
		for (int i = ((left.length)-1); i>(-1); i--){
			if (!(left[i].equals(""))){
				JTextField x = new JTextField(left[i]);
				x.setEditable(false);
				// TRY TO POSITION THE TEXT FIELD
				if ((right[0].equals("")) && (centre[0].equals(""))){
					x.setLocation(180, pos);
					//x.setAlignmentX(180);
					//x.setAlignmentY(pos);
					//x.setLocation((p.getX()+180), (p.getY()+pos));
					//x.setSize(140, 30);
					//x.setBounds(180, pos, 140, 30);
				}
				x.setHorizontalAlignment(JTextField.CENTER);
				
				p.add(x);
				//x.invalidate();
				//x.validate();
				//x.repaint();
				pos += 100;				
			}
		}
		p.setBackground(Color.WHITE);
		//testPanel(p);
		scroll.setViewportView(p);
		return scroll;	
		// SCROLL IS LATER ADDED TO A JFRAME
	}

The default layout manager is FlowLayout, which is what your JPanel is currently using since you have not changed it. You can change the positions of those text fields all day but FlowLayout won't respect them. It wasn't designed for that.

I would recommend using a BoxLayout or GridLayout if you just need to stack them, but you may need to examine the capabilities of other layout managers and play around with them to get things working like you desire. Take a look through this tutorial on laying out components:
http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html

Thanks Ezzaral. Do you know if there's any way I can change the layout to null, then add the text fields on top using absolute addressing? (Tried this and nothing appears).

No worries, sorted it using that link, thank you! :)

This question has already been answered. Start a new discussion instead.