Submit Your Site To The Web's Top 50 Search Engines for Free!       ExactSeek: Relevant Web Search


Flag Counter

Total Pageviews

Saturday, November 17, 2012

JAVA FlowLayout

The FlowLayout is the simplest layout manager. By default, it puts all the components into a row in the order they are added to the container from left-to-right. If the row meets the end of the container then a new row is started beneath.

Component Flow

The order of the components can be switched so that they appear from right-to-left by using the container's setComponentOrientation method:
JPanel buttonPanel = new JPanel();
The setComponentOrientation method accepts the following field values from the ComponentOrientation class:
  • LEFT_TO_RIGHT - components flow from the left-to-right.
  • RIGHT_TO_LEFT - components flow from the right-to-left.
  • UNKNOWN - specifies that the flow hasn't been set. In practice, if this value is set when the container is shown it will show a component flow of left-to-right.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DemoFlowLayout extends JFrame{
    public DemoFlowLayout(){
        setTitle("Demo Flow Layout");
        JPanel p1 = new JPanel();
        FlowLayout f = new FlowLayout(FlowLayout.LEFT, 10,20);
        JLabel lblFirstName = new JLabel("First Name");
        JLabel lblMiddleName = new JLabel("Middle Name");
        JLabel lblLastName = new JLabel("Last Name");
        JTextField txtFirstName = new JTextField(10);
        JTextField txtMiddleName = new JTextField(10);
        JTextField txtLastName = new JTextField(10);
        JButton btnKeluar = new JButton("Exit");
        p1.add(lblFirstName); p1.add(txtFirstName);
        p1.add(lblMiddleName); p1.add(txtMiddleName);
        p1.add(lblLastName); p1.add(txtLastName);
        btnKeluar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
    public static void main(String[]args) {
        JFrame frame = new DemoFlowLayout();

the result of the source code


Post a Comment