Thursday, November 17, 2011

Create New JTextField

    package org.best.example;
    /*
            Create New JTextField Example
            This java example shows how to create new JTextField using
            Java Swing JTextField class.
    */
    
    
    import java.awt.FlowLayout;
    
    import javax.swing.JApplet;
    import javax.swing.JTextField;
    
    /*
    <applet code="CreateNewJTextFieldExample" width=200 height=200>
    </applet>
    */
    
    public class CreateNewJTextFieldExample extends JApplet{
    
            public void init(){
                  
                    //set flow layout for the applet
                    this.getContentPane().setLayout(new FlowLayout());
                  
                    /*
                     * To create new JTextField use,
                     * JTextField()
                     * constructor.
                     *
                     * This will create new empty JTextField.
                     */
                  
                    JTextField field1 = new JTextField();
                  
                    /*
                     * To create new JTextField with default text, use
                     * JTextField(String text)
                     * constructor.
                     *
                     * This will create new JTextField with default text.
                     */
                    JTextField field2 = new JTextField("JTextField Default Text");
                  
                    /*
                     * To create new JTextField with specified number of columns, use
                     * JTextField(int columns)
                     * constructor.
                     *
                     * This will create new empty JTextField with specified number of
                     * columns.
                     */
                    JTextField field3 = new JTextField(10);
                  
                    /*
                     * To create new JTextField with default text and columns use
                     * JTextField(String text, int columns)
                     * constructor.
                     *
                     * This will create new JTextField with default text and specified
                     * number of columns.
                     */
                    JTextField field4 = new JTextField("JTextField Default Text", 20);
                  
                    /*
                     * Add text field to the applet
                     */
                    add(field1);
                    add(field2);
                    add(field3);
                    add(field4);
                  
            }
    }

No comments: