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

Visitors

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();
buttonPanel.setComponentOrientation
(ComponentOrientation.RIGHT_TO_LEFT);
 
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);
        p1.setLayout(f);
       
        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);
        add(p1);
       
        btnKeluar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        });
    }
   
    public static void main(String[]args) {
        JFrame frame = new DemoFlowLayout();
        frame.setVisible(true);
        frame.setSize(200,300);
        frame.setDefaultCloseOperation(2);
    }
}

the result of the source code




0 comments

Post a Comment

loading