π Introduction
In GUI programming, placing components randomly leads to messy and unusable interfaces π΅
π Thatβs why Java provides Layout Managers β they automatically arrange components in a structured way π―
In this blog, you will learn:
- What are layout managers
- Different types of Swing layouts
- 5+ complete runnable programs
- Proper explanation of each
π§ What is a Layout Manager?
π A Layout Manager controls:
- Position π
- Size π
- Alignment
of components inside a container (like JFrame or JPanel)
π₯ Why Layout Managers are Important?
- Avoid overlapping β
- Make UI responsive π±
- Easy to manage components
π§± 1. FlowLayout
π Features
- Left β Right arrangement
- Automatically wraps
β Program 1: FlowLayout Example
import javax.swing.*;
import java.awt.*;
public class FlowLayoutDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("FlowLayout Example");
frame.setLayout(new FlowLayout());
frame.add(new JButton("One"));
frame.add(new JButton("Two"));
frame.add(new JButton("Three"));
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
π Explanation
- Components are placed in a row
- Automatically move to next line
π§± 2. BorderLayout
π Features
- 5 regions:
- North
- South
- East
- West
- Center
β Program 2: BorderLayout Example
import javax.swing.*;
import java.awt.*;
public class BorderLayoutDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("BorderLayout Example");
frame.setLayout(new BorderLayout());
frame.add(new JButton("North"), BorderLayout.NORTH);
frame.add(new JButton("South"), BorderLayout.SOUTH);
frame.add(new JButton("East"), BorderLayout.EAST);
frame.add(new JButton("West"), BorderLayout.WEST);
frame.add(new JButton("Center"), BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
π Explanation
- Each region holds one component
- Center expands automatically
π§± 3. GridLayout
π Features
- Rows Γ Columns
- Equal size components
β Program 3: GridLayout Calculator UI
import javax.swing.*;
import java.awt.*;
public class GridLayoutDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("GridLayout Example");
frame.setLayout(new GridLayout(3, 3));
for (int i = 1; i <= 9; i++) {
frame.add(new JButton(String.valueOf(i)));
}
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
π Explanation
- Creates grid structure
- All buttons same size
π§± 4. BoxLayout
π Features
- Vertical or Horizontal alignment
β Program 4: Vertical Layout
import javax.swing.*;
public class BoxLayoutDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("BoxLayout Example");
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(new JButton("Button 1"));
frame.add(new JButton("Button 2"));
frame.add(new JButton("Button 3"));
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
π Explanation
- Components arranged vertically
- Good for forms
π§± 5. Null Layout (Manual Layout)
π Features
- No layout manager
- Manual positioning
β Program 5: Manual Placement
import javax.swing.*;
public class NullLayoutDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Null Layout");
frame.setLayout(null);
JButton btn = new JButton("Click Me");
btn.setBounds(100, 50, 120, 30);
frame.add(btn);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
π Explanation
setBounds(x, y, width, height)- Not recommended for real apps β οΈ
π§± 6. Combined Layout Example (Mini Form)
β Program 6: Simple Form
import javax.swing.*;
import java.awt.*;
public class FormDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Form Example");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
panel.add(new JLabel("Name:"));
panel.add(new JTextField());
panel.add(new JLabel("Age:"));
panel.add(new JTextField());
panel.add(new JButton("Submit"));
frame.add(panel);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
π Explanation
- Uses JPanel + GridLayout
- Real-world form structure
β οΈ Common Mistakes
β Not setting layout
π Components overlap
β Using null layout everywhere
π Not responsive β
β Adding multiple components to same BorderLayout region
π Only one will show
π€― Common Confusions
π€ Which layout to use?
| Situation | Layout |
|---|---|
| Simple UI | FlowLayout |
| Structured UI | BorderLayout |
| Form/Grid | GridLayout |
| Vertical list | BoxLayout |
πΌ Interview Questions
β What is Layout Manager?
π Controls component arrangement
β Default layout of JFrame?
π BorderLayout
β Why avoid null layout?
π Not responsive
π― Best Practices
βοΈ Always use layout managers
βοΈ Avoid null layout
βοΈ Use panels for complex UI
βοΈ Combine layouts
π Conclusion
Layout managers are the backbone of GUI design π―
In this blog, you learned:
- βοΈ Types of layouts
- βοΈ 5+ practical programs
- βοΈ When to use each layout
π‘ Final Thought
π A good layout = Clean UI = Better user experience π―
π₯ One-Line Summary
π βLayout managers control how components are arranged in a Swing application.β
π» Happy Coding! π
Next step: Event Delegation Model π₯