Using swing in Java
In the previous chapter, we use javax.swing package to make our GUI. But, there are a few more components to discuss.
Using swing in Java

In the previous chapter, we use javax.swing package to make our GUI. But, there are a few more components to discuss.
Swing Components
Some swing components that we learned in the previous article are subclasses of javax.swing.JComponent class. In a swing, all the components are capable of holding other swing components. That’s why we are able to put interactive components like JButton and JTextField to the background components like JFrame or JPanel. The funny this is that we can revise the order as well means that JButton is capable to hold a JPanel. Likewise, JPanel that we are used to grouping but, the components can register for the events as well.
Layout Manager
Layout Manager is a Java object associated with the background components and it controls the size and placement of the It contained components. For example, assume that JFrame contains JPanel and the JPanel contains JButton then, the layout manager of JPanel controls the JButton placement and size. Think that the JPanel holds another JPanel we called as jpanel2 and the jpanel2 contains the JButton. Then only the layout manager of jpanel2 can control the JButton. The layout manager of the outer JPanal can’t control the JButton size and placement.
And the important thing is each background component has its own layout manager and the layout manager is come in several flavors. As well as, Layout Manager has its own policies it’s different from one layout manager to another layout manager. For example, assume that We add two JPanel to the Frames and one JPanel’s layout manager can arrange components inside it in a grid format at the same time another JPanel’s layout manager can arrange components in stack format. So, each layout manager of the background layout is unique on its own.
Type of Layout Managers
- Border Layout — This is the default layout manager for a frame. This layout manager is divided into five regions and one region can have one component.

2. Flow Layout — This is the default layout manager for a panel. Each component of this layout is in the size that the component wants. If the component isn’t fit horizontally, then it drops to the next line.

3. Box Layout — It’s almost like the flow layout means the components have their own size and can place as we want. But, this layout manager can stack the components horizontally or vertically. This layout manager has automatic component wrapping feature.

In the previous article, we discuss the five regions of the border layout.
This is how BorderLayout works for the five buttons. In the Center region takes up more space.

public void init(){
JFrame frame = new JFrame();
button1 = new JButton();
button1.setText("West");
button2 = new JButton();
button2.setText("Center");
button3 = new JButton();
button3.setText("East");
button4 = new JButton();
button4.setText("North");
button5 = new JButton();
button5.setText("South");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(BorderLayout.WEST,button1);
frame.getContentPane().add(BorderLayout.CENTER,button2);
frame.getContentPane().add(BorderLayout.EAST,button3);
frame.getContentPane().add(BorderLayout.NORTH,button4);
frame.getContentPane().add(BorderLayout.SOUTH,button5);
frame.setSize(600,300);
frame.setVisible(true);
}
And The below result is what I got for the same five buttons in FlowLayout.

public void flowLayoutInit(){
JFrame frame = new JFrame();
button1 = new JButton();
button1.setText(“West”);
button2 = new JButton();
button2.setText(“Center”);
button3 = new JButton();
button3.setText(“East”);
button4 = new JButton();
button4.setText(“North”);
button5 = new JButton();
button5.setText(“South”);
JPanel panel = new JPanel();
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
frame.getContentPane().add(panel);
frame.setSize(600,300);
frame.setVisible(true);
}
And This is what happened when we use the Box layout for panel.

public void BoxPanelInit(){
JFrame frame = new JFrame();
button1 = new JButton();
button1.setText("West");
button2 = new JButton();
button2.setText("Center");
button3 = new JButton();
button3.setText("East");
button4 = new JButton();
button4.setText("North");
button5 = new JButton();
button5.setText("South");
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
frame.getContentPane().add(BorderLayout.EAST,panel);
frame.setSize(600,300);
frame.setVisible(true);
}
Happy Learning!
메타데이터
- post_id
- ed9ba56fbd4
- slug
- 13-using-swing-in-java-ed9ba56fbd4
- url
- https://medium.com/@thilini_/13-using-swing-in-java-ed9ba56fbd4
- canonical_url
- https://medium.com/@thilini_/13-using-swing-in-java-ed9ba56fbd4
- author_url
- https://medium.com/@thilini_
- status
- ok
- fetched_at
- 2026-07-26 21:01:39