Saturday, November 19, 2011

Create Read Only JTextField

    package org.best.example;
    /*
            Create Read Only JTextField Example
            This java example shows how to make JTextField read only or non editable
            using Java Swing JTextField class.
    */
    
    
    import java.awt.FlowLayout;
    
    import javax.swing.JApplet;
    import javax.swing.JTextField;
    
    /*
    <applet code="CreateReadOnlyJTextFieldExample" width=200 height=200>
    </applet>
    */
    public class CreateReadOnlyJTextFieldExample extends JApplet{
          
            public void init(){
                  
                    //set flow layout for the applet
                    this.getContentPane().setLayout(new FlowLayout());
                  
                    //create new JTextField
                    JTextField field = new JTextField("Read Only JTextField Example");
                  
                    /*
                     * To make JTextField read only or non editable use,
                     * void setEditable(boolean editable)
                     * method with false argument.
                     */
                  
                    field.setEditable(false);
                  
                    /*
                     * To make the JTextField editable, use
                     * void seEditable(boolean editable)
                     * with true argument.
                     */
                  
                    //field.setEditable(true);
                  
                    add(field);
                  
            }
    
    }

No comments: