Please help me to expound how to do layout management. You may choose any layout manager and expound it. I'm doing self teaching most of the time and it is not fun! Please help me ho to arrange.

Also which layout is mostly used

Official Java Sun tutorial Using Layout Managers

As for which layout is used mostly? Dunno normally I use GridBagLayout in combination in some others FlowLayout or BorderLayout. One suggestion, do not try to arrange all element through one layout. Divide it on smaller section on which you apply layout as need it and fit it in main layout that will organize these groups

PS: If you have chance get your self Swing: A Beginner's Guide By Herbert Schildt. I personally like it if that counts for something

Edited 6 Years Ago by peter_budo: n/a

So, according to the doc you have to follow this steps
1. Get content pane
2. Set layout manager for that content pane
3. add components

If so does that mean I have to use many panels for single program that needs many layouts?

You do not have to use many layouts you can do it with single, but my pass experiences and other people too is that it is good to mix layouts as you need it.
It is same as web page development, there can be tables inside table, containers inside container. Some have alignment to the left other to the middle, some have margin or padding. Hope you understand what I mean

can you elaborate a little bit in new bee's terms?
I find Gridbag a little confusing. Java layout is a bit convoluted though ;)

Well I made mistake and did not tell you that GridBagLayout is most complex of them, so you may want to learn first about the other ones. Try to read through the tutorial I linked previously.

As for GridBagLayout here is example from Swing - A Beginner's Guide

import java.awt.*;
import javax.swing.*;

class GBDemo{
	GBDemo(){
		
		//Create a new JFrame container
		JFrame jfrm = new JFrame("GridBagLayout Demo");
		
		//Create the grid bag
		GridBagLayout gbag = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		
		//Set the grid bag as the layout manager for the content pane
		jfrm.getContentPane().setLayout(gbag);
		
		//Give the frame initial size
		jfrm.setSize(240, 240);
		
		//Terminate the program when the user closes the application
		jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Make the labels
		JLabel jlabOne = new JLabel("Button Group One");
		JLabel jlabTwo = new JLabel("Button Group Two");
		JLabel jlabThree = new JLabel("Button Group Three");
		
		// Make the buttons
		JButton jbtnOne = new JButton("One");
		JButton jbtnTwo = new JButton("Two");
		JButton jbtnThree = new JButton("Three");
		JButton jbtnFour = new JButton("Four");
		
		//Set button size
		Dimension btnDim = new Dimension(100, 25);
		jbtnOne.setPreferredSize(btnDim);
		jbtnTwo.setPreferredSize(btnDim);
		jbtnThree.setPreferredSize(btnDim);
		jbtnFour.setPreferredSize(btnDim);
		
		//Make the check boxes
		JCheckBox jcbOne = new JCheckBox("Option One");
		JCheckBox jcbTwo = new JCheckBox("Option Two");
		
		/*
		 *Define the grid bag
		 */
		 
		/*
		 *By using a weight of 1.0, the components divide the
		 *horizontal space. however, because weighty uses its
		 *default value of 0.0, the component remain centered
		 *in the vertical space.
		 */
		 gbc.weightx = 1.0;
		 
		 //Define the grid location for each component
		 
		 //Put the button labels in location 0,0 and 1,0
		 gbc.gridx = 0;
		 gbc.gridy = 0;
		 gbag.setConstraints(jlabOne, gbc);
		 
		 gbc.gridx = 1;
		 gbc.gridy = 0;
		 gbag.setConstraints(jlabTwo, gbc);
		 
		 //Add some space around the buttons
		 gbc.insets = new Insets(4,4,4,4);
		 
		 //Put the buttons at locations 0,1 and 1,1 and so on
		 gbc.gridx = 0;
		 gbc.gridy = 1;
		 gbag.setConstraints(jbtnOne, gbc);
		 
		 gbc.gridx = 1;
		 gbc.gridy = 1;
		 gbag.setConstraints(jbtnTwo, gbc);
		 
		 gbc.gridx = 0;
		 gbc.gridy = 2;
		 gbag.setConstraints(jbtnThree, gbc);
		 
		 gbc.gridx = 1;
		 gbc.gridy = 2;
		 gbag.setConstraints(jbtnFour, gbc);
		 
		 /*
		  *Have the last label and the two chcek boxes
		  *use the remaining space
		  */
		 gbc.gridwidth = GridBagConstraints.REMAINDER;
		 
		 //Use an inset of 10 from the top for the label
		 gbc.insets = new Insets(10, 0, 0, 0);
		 gbc.gridx = 0;
		 gbc.gridy = 3;
		 gbag.setConstraints(jlabThree, gbc);
		 
		 // No insets for the check boxes
		 gbc.insets = new Insets(0, 0, 0, 0);
		 gbc.gridx = 0;
		 gbc.gridy = 4;
		 gbag.setConstraints(jcbOne, gbc);
		 
		 gbc.gridx = 0;
		 gbc.gridy = 5;
		 gbag.setConstraints(jcbTwo, gbc);
		 
		 //Add everything to the content pane
		 jfrm.getContentPane().add(jlabOne);
		 jfrm.getContentPane().add(jlabTwo);
		 jfrm.getContentPane().add(jbtnOne);
		 jfrm.getContentPane().add(jbtnTwo);
		 jfrm.getContentPane().add(jbtnThree);
		 jfrm.getContentPane().add(jbtnFour);
		 jfrm.getContentPane().add(jlabThree);
		 jfrm.getContentPane().add(jcbOne);
		 jfrm.getContentPane().add(jcbTwo);
		 
		 //Display the frame
		 jfrm.setVisible(true);
	}
	
	public static void main(String[] args){
		// Create the frame on the event dispatching thread
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				new GBDemo();
			}
		});
	}
}
This article has been dead for over six months. Start a new discussion instead.