tags:

views:

63

answers:

6

I have a JFrame in which i have to insert JLabels, textfields and JButtons. I am able to these but how can i adjust them to the required position, i want to add one label and textfield in one row and then nxt label and textfield in the next row but they are coming in the same horizontal line. i have used flowLayout with the JFrame. please tell me how to adjust them accordingly. thanks

A: 

had a look at gridbaglayout? Should serve your purpose.

Ryan Fernandes
thanks a lot... it has solved my problem.
Thanks somya, maybe you could mark an answer as 'accepted' so as to close this question
Ryan Fernandes
A: 

A GridLayout may be what you want, or a combination of a GridLayout and a FlowLayout. Look at the LayoutManager tutorial to get a better idea of when and how to use and combine the various layout managers.

Michael Borgwardt
+4  A: 

The key to distributing components in a Container in Swing is the Layout Manager. There are various types out there. To do what you are looking for, you might want to consider the GridLayout. It is pretty easy to set up. You first need to create the layout. The following will create a two columned layout with as many rows as you provide:

GridLayout gl = new GridLayout(0,2);

Then you apply it to your panel:

JPanel panel = new JPanel(gl);

Then you add your items:

panel.add(textfield1);
panel.add(button1);
panel.add(textfield2);
panel.add(button2);

The GridLayout will handle moving from row to row after you fill in the columns with components.

akf
A: 

You need to study the various types of layouts swing provides. Also you can have a look at FormLayout,provide by JGoodies. I prefer to use this than swing layouts as i find it easy to code and less lines of code

harshit
Also MigLayout (http://www.miglayout.com/) - best layout manager I've used.
Nate
A: 

You are using the default Swing Layout Manager. If you want different behaviour (which is very reasonable) then you need to use another LayoutManager. Several exists both from Sun and "out there".

In order for you to be able to choose, you need to know how they work. I can strongly recommend using the Java Tutorial for this:

http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

Let us know if you need more than provided by e.g. nested BorderLayout's or TableLayout.

Thorbjørn Ravn Andersen
A: 

If you want to be able to position the UI elements in your application (nearly) absolutely, consider using a decent GUI builder like Matisse in NetBeans or Swing UI Designer in IntelliJ IDEA.

Ilya Boyandin