Showing posts with label Java - Applet JTextField. Show all posts
Showing posts with label Java - Applet JTextField. Show all posts

Thursday, November 24, 2011

JTextField Horizontal Alignment

    package org.best.example;
    /*
            JTextField Horizontal Alignment Example
            This java example shows how to get or set horizontal alignment of
            JTextField's text using Java Swing JTextField class.
    */
    
    
    import java.awt.FlowLayout;
    import javax.swing.JApplet;
    import javax.swing.JTextField;
    
    /*
    <applet code="JTextFieldHorizontalAlignmentExample" width=200 height=200>
    </applet>
    */
    
    public class JTextFieldHorizontalAlignmentExample extends JApplet{
          
            public void init(){
                  
                    //set flow layout for the applet
                    this.getContentPane().setLayout(new FlowLayout());
    
                    //create new JTextField
                    JTextField field = new JTextField("JTextField Horizontal Alignment Example", 10);
                  
                    /*
                     * To get horizontal alignment of JTextBox's text use,
                     * int getHorizontalAlignment()
                     * method of JTextField class.
                     *
                     * Return value is one of the following values,
                     * JTextField.LEFT, JTextField.CENTER, JTextField.RIGHT
                     * JTextField.LEADING, Or JTextField.TRAILING
                     */
                  
                    int horizontalAlignment = field.getHorizontalAlignment();
                  
                    switch(horizontalAlignment){
                  
                            case JTextField.LEFT:
                                    field.setText("Left");
                                    break;
                            case JTextField.RIGHT:
                                    field.setText("Right");
                                    break;
                            case JTextField.CENTER:
                                    field.setText("Center");
                                    break;
                            case JTextField.LEADING:
                                    field.setText("Leading");
                                    break;
                            case JTextField.TRAILING:
                                    field.setText("Trailing");
                                    break;
                    }
                    /*
                     * To set horizontal alignment of JTextField's text use,
                     * void setHorizontalAlignment(int alignment)
                     * method of JTextField class.
                     */
                  
                    //field.setHorizontalAlignment(JTextField.CENTER);
                  
                    add(field);
                  
            }
    }

Wednesday, November 23, 2011

JTextField Disable Enable

    package org.best.example;
    /*
            JTextField Disable Enable Example
            This java example shows how to enable or disable JTextField
            using Java Swing JTextField class.
    */
    
    
    import java.awt.FlowLayout;
    import javax.swing.JApplet;
    import javax.swing.JTextField;
    
    /*
    <applet code="JTextFieldDisableExample" width=200 height=200>
    </applet>
    */
    
    public class JTextFieldDisableExample extends JApplet{
          
            public void init(){
                  
                    //set flow layout for the applet
                    this.getContentPane().setLayout(new FlowLayout());
    
                    //create new JTextField
                    JTextField field = new JTextField("JTextField Disable Enable Example",20);
                  
                    /*
                     * To disable JTextField use
                     * void setEnabled(boolean enabled)
                     * method with false argument.
                     */
                  
                    field.setEnabled(false);
                  
                    /*
                     * To set the disabled JTextField enable, use the same method
                     * with the true argument.
                     */
                  
                    //field.setEnabled(true);
                  
                    add(field);
            }
    }

Tuesday, November 22, 2011

JTextField Columns

    package org.best.example;
    /*
            JTextField Columns Example
            This java example shows how to get or set JTextField columns using
            Java Swing JTextField class.
    */
    
    
    import java.awt.FlowLayout;
    
    import javax.swing.JApplet;
    import javax.swing.JTextField;
    
    /*
    <applet code="JTextFieldColumnsExample" width=200 height=200>
    </applet>
    */
    
    public class JTextFieldColumnsExample extends JApplet{
    
            public void init(){
                  
                    //set flow layout for the applet
                    this.getContentPane().setLayout(new FlowLayout());
                  
                    //create new JTextField
                    JTextField field = new JTextField("JTextField Columns Example");
                  
                    /*
                     * To set columns for JTextField use,
                     * void setColumns(int columns)
                     * method of JTextField class.
                     */
                  
                    field.setColumns(20);
                  
                    /*
                     * To get current columns of JTextField use,
                     * int getColumns()
                     * method of JTextField class.
                     */
                  
                    int columns = field.getColumns();
                  
                    add(field);
                  
            }
    }

Monday, November 21, 2011

Get X and Y Coordinates of JTextField

    package org.best.example;
    /*
            Get X and Y Coordinates of JTextField Example
            This java example shows how to get X and Y coordinates of JTextField
            using Java Swing JTextField class.
    */
    
    
    import java.awt.FlowLayout;
    
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    /*
    <applet code="JTextFieldGetXYCoordinatesExample" width=200 height=200>
    </applet>
    */
    
    public class JTextFieldGetXYCoordinatesExample extends JApplet{
    
            public void init(){
                  
                    //set flow layout for the applet
                    this.getContentPane().setLayout(new FlowLayout());
    
                    //create new JTextField
                    JTextField field = new JTextField("JTextField X Y Coordinates Example",20);
                  
                    /*
                     * To get X coordinate of JTextField use,
                     * int getX()
                     * method.
                     *
                     * To get Y coordinate of JTextField use,
                     * int getY()
                     * method.
                     */
                  
                    int x = field.getX();
                    int y = field.getY();
    
    
            }
    }

Sunday, November 20, 2011

Get Current Caret Position in JTextField

    package org.best.example;
    /*
            Get Current Caret Position in JTextField Example
            This java example shows how to get current position of caret in JTextField
            using Java Swing JTextField class.
    */
    
    
    import java.awt.FlowLayout;
    
    import javax.swing.JApplet;
    import javax.swing.JTextField;
    import javax.swing.event.CaretEvent;
    import javax.swing.event.CaretListener;
    
    /*
    <applet code="GetCaretPositionInJTextFieldExample" width=200 height=200>
    </applet>
    */
    
    /*
     * Implement CaretListener in order to listen for caret position
     * change event.
     */
    public class GetCaretPositionInJTextFieldExample extends JApplet implements CaretListener{
          
            public void init(){
                  
                    //set flow layout for the applet
                    this.getContentPane().setLayout(new FlowLayout());
                  
                    //create new JTextField
                    JTextField field = new JTextField("JTextField Caret Position Example");
                  
                    add(field);
                  
                    //add CaretListener in order to listen for caret position changes
                    field.addCaretListener(this);
            }
    
            //override caretUpdate event to capture caret position changes
            public void caretUpdate(CaretEvent e) {
                  
                    /*
                     * To get the current position of caret use,
                     * int getDot()
                     * method of CaretEvent class.
                     */
                  
                    int position = e.getDot();
                  
                    //show current position in the status bar of an applet
                    getAppletContext().showStatus("Caret Position :" + position);
            }
    
          
    }

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);
                  
            }
    
    }

Friday, November 18, 2011

Change Font of JTextField

    package org.best.example;
    /*
            Change Font of JTextField Example
            This java example shows how to change font of JTextField's text
            using Java Swing JTextField class.
    */
    
    
    import java.awt.FlowLayout;
    import java.awt.Font;
    import javax.swing.JApplet;
    import javax.swing.JTextField;
    
    /*
    <applet code="JTextFieldFontExample" width=200 height=200>
    </applet>
    */
    
    public class JTextFieldFontExample extends JApplet{
          
            public void init(){
                  
                    //set flow layout for the applet
                    this.getContentPane().setLayout(new FlowLayout());
    
                    //create new JTextField
                    JTextField field = new JTextField("JTextField Change Font Example",30);
                  
                    /*
                     * To set custom font for JTextField use,
                     * void setFont(Font font)
                     * method.
                     */
                  
                    //create new Font
                    Font font = new Font("Courier", Font.BOLD,12);
                  
                    //set font for JTextField
                    field.setFont(font);
    
                    add(field);
            }
    }

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);
                  
            }
    }

Wednesday, November 16, 2011

JTextField Show Hide

    package org.best.example;
    /*
            JTextField Show Hide Example
            This java example shows how to show or hide JTextField
            using Java Swing JTextField class.
    */
    
    
    import java.awt.FlowLayout;
    import javax.swing.JApplet;
    import javax.swing.JTextField;
    
    /*
    <applet code="JTextFieldShowHideExample" width=200 height=200>
    </applet>
    */
    public class JTextFieldShowHideExample extends JApplet{
    
            public void init(){
                  
                    //set flow layout for the applet
                    this.getContentPane().setLayout(new FlowLayout());
    
                    //create new JTextField
                    JTextField field = new JTextField("JTextField Show Hide Example", 10);
                  
                    /*
                     * To hide JTextField use
                     * void setVisible(boolean visible)
                     * method with false argument.
                     */
                  
                    field.setVisible(false);
                  
                    /*
                     * To set the hidden JTextField visible, use the same method
                     * with the true argument.
                     */
                  
                    //field.setVisible(true);
                  
                    add(field);
            }
    }

Tuesday, November 15, 2011

JTextField ToolTip

    package org.best.example;
    /*
            JTextField ToolTip Example
            This java example shows how to set of get ToolTip for JTextField
            using Java Swing JTextField class.
    */
    
    
    import java.awt.FlowLayout;
    import javax.swing.JApplet;
    import javax.swing.JTextField;
    
    /*
    <applet code="JTextFieldTooltipExample" width=200 height=200>
    </applet>
    */
    public class JTextFieldTooltipExample extends JApplet{
          
            public void init(){
                  
                    //set flow layout for the applet
                    this.getContentPane().setLayout(new FlowLayout());
    
                    //create new JTextField
                    JTextField field = new JTextField("JTextField ToolTip Example",20);
                  
                    /*
                     * To set JTextField's ToolTip use
                     * void setToolTipText(String toolTip)
                     * method.
                     */
                  
                    field.setToolTipText("This is JTextField's ToolTip");
                  
                    /*
                     * To get the ToolTip of JTextField , use
                     * String getToolTipText()
                     * method of JTextField class.
                     */
                  
                    String toolTip = field.getToolTipText();
                  
                    add(field);
            }
    }

Monday, November 14, 2011

Set Background Color of JTextField

    package org.best.example;
    /*
            Set Background Color of JTextField Example
            This java example shows how to set background color of JTextField
            using Java Swing JTextField class.
    */
    
    
    import java.awt.Color;
    import java.awt.FlowLayout;
    
    import javax.swing.JApplet;
    import javax.swing.JTextField;
    
    /*
    <applet code="JTextFieldBackgroundColorExample" width=200 height=200>
    </applet>
    */
    
    public class JTextFieldBackgroundColorExample extends JApplet{
    
            public void init(){
                  
                    //set flow layout for the applet
                    this.getContentPane().setLayout(new FlowLayout());
    
                    //create new JTextField
                    JTextField field = new JTextField("JTextField Background Color Example",20);
                  
                    //create new custom color
                    Color color = Color.GRAY;
                  
                    /*
                     * To set background color of JTextField's text use,
                     * void setBackground(Color color)
                     * method of JTextField class.
                     */
                  
                    field.setBackground(color);
    
                    add(field);
            }
    }

Sunday, November 13, 2011

Set Foreground Color of JTextField

    package org.best.example;
    /*
            Set Foreground Color of JTextField Example
            This java example shows how to set foreground color of JTextField's text
            using Java Swing JTextField class.
    */
    
    
    import java.awt.Color;
    import java.awt.FlowLayout;
    
    import javax.swing.JApplet;
    import javax.swing.JTextField;
    
    /*
    <applet code="JTextFieldForegroundColorExample" width=200 height=200>
    </applet>
    */
    
    public class JTextFieldForegroundColorExample extends JApplet{
          
            public void init(){
                  
                    //set flow layout for the applet
                    this.getContentPane().setLayout(new FlowLayout());
    
                    //create new JTextField
                    JTextField field = new JTextField("JTextField Foreground Color Example",20);
                  
                    //create new custom color
                    Color color = Color.RED;
                  
                    /*
                     * To set foreground color of JTextField's text use,
                     * void setForeground(Color color)
                     * method of JTextField class.
                     */
                  
                    field.setForeground(color);
    
                    add(field);
            }
    }

Saturday, November 12, 2011

Set JTextField Border

    package org.best.example;
    /*
            Set JTextField Border Example
            This java example shows how to create a border for JTextField using
            Java Swing JTextField class.
    */
    
    
    import java.awt.Color;
    import java.awt.FlowLayout;
    
    import javax.swing.BorderFactory;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.border.Border;
    
    /*
    <applet code="JTextFieldSetBorderExample" width=200 height=200>
    </applet>
    */
    
    
    public class JTextFieldSetBorderExample extends JApplet{
          
            public void init(){
                  
                    //set flow layout for the applet
                    this.getContentPane().setLayout(new FlowLayout());
    
                    //create new JTextField
                    JTextField field = new JTextField("JTextField Border Example",20);
                  
                    /*
                     * Create new custom border for JTextField
                     */
                    Border border = BorderFactory.createLineBorder(Color.red);
                  
                    /*
                     * To set JTextField's border use,
                     * void setBorder(Border b)
                     * method.
                     */
                  
                    field.setBorder(border);
                  
                    add(field);
    
            }
    
    }