The FlowLayout manager is the simplest layout manager in the Java Swing toolkit. It is the default layout manager for the JPanel component.
The implicit layout manager of the JPanel component is FlowLayout. We do not have to set it manually.
There are three constructors available for the FlowLayout manager. The first one creates a manager with implicit values. Centered with 5px horizontal and vertical spaces. The others allow specifying those parameters.
FlowLayout()
FlowLayout(int align)
FlowLayout(int align, int hgap, int vgap)
Java Swing FlowLayout Example
The following example demonstrates the usage of the FlowLayout manager. The example shows a button, a tree component, and a text area component in the window.
package net.javaguides.javaswing.examples;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTree;
import java.awt.Dimension;
import java.awt.EventQueue;
/**
* Class demonstrates the usage of FlowLayout manager.
* @author javaguides.net
*
*/
public class FlowLayoutExample extends JFrame {
private static final long serialVersionUID = 1L;
public FlowLayoutExample() {
initUI();
}
private void initUI() {
JPanel panel = new JPanel();
JButton button = new JButton("button");
panel.add(button);
JTree tree = new JTree();
panel.add(tree);
JTextArea area = new JTextArea("Text Area");
area.setPreferredSize(new Dimension(200, 200));
panel.add(area);
add(panel);
pack();
setTitle("FlowLayout example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
FlowLayoutExample ex = new FlowLayoutExample();
ex.setVisible(true);
});
}
}
Output
Related Swing Examples
- Java Swing Exit Button - In this post, I show you how to exit a Swing application when clicking on the exit button.
- Swing ToolTip Tutorial with Example - In this tutorial, we will learn how to add tooltip text to a Swing component.
- Java Swing BorderLayout Example - In this example, we will learn how to use BorderLayout in GUI/swing based applications.
- Java Swing GridLayout Tutorial with Examples - In this tutorial, we will learn how to use GridLayout in GUI/swing based applications.
- Swing Mouse Move Events using MouseMotionAdapter - In this tutorial, we will learn how to receive mouse motion events using MouseMotionAdapter.
- Java Swing CheckBox Example - In this post, I show you how to use JCheckBox class to create a Radio button in a Swing-based application.
- Java Swing Radio Button Example - In this post, I show you how to use JRadioButton class to create a Radio button in a Swing-based application.
- Java Swing Progress Bar Example - In this post, I show you how to create a progress bar using the JProgressBar component in swing-based applications.
- Java Swing Combo Box Example - In this post, I show you how to create a combo box using a JComboBox component in swing-based applications.
- Java Swing Slider Example - In this post, I show you how to create a slider using the JSlider component in swing-based applications.
- Java Swing Toggle Button Example - In this post, we will learn how to create a toggle button using the JToggleButton component in swing-based applications.
Comments
Post a Comment
Leave Comment