Create TextBox in java

Discussion in 'Java' started by satyedra pal, Apr 15, 2010.

  1. satyedra pal

    satyedra pal New Member

    Joined:
    Mar 26, 2010
    Messages:
    93
    Likes Received:
    1
    Trophy Points:
    0
    TextBox is used in two ways TextField and TextArea.The TextField and TextArea are two different classes in Java for input text data and also displaying text data.The TextField class manage only one line of string or text and TextArea class manages multiple lines or multiple string.
    We are giving the example of TextField class example.In this example we are giving the size of the textField within bracket.

    TextField txt = new TextField(25);
    If we want to show the text for the TextField then we write following code:


    TextField txt = new TextField("your full name");
    For entering text in TextField we use "setText()" method.And for finding the input from the textField we use "getText()"method.
    For example: For entering txt.setText("Bye"); for reteriving txt.getText();

    Now we are giving an example of a class for TextBox implementation.


    import java.applet.*;
    import java.awt.*;

    public class txtdemo extends Applet

    {
    TextField txt1 = new TextField(25);
    TextField passtxt1 = new TextField(10);
    TextField txt2 = new TextField(25);
    TextField passtxt2 = new TextField(10);
    Button button1=new Button("after entering press");


    public void init()
    {
    add(txt1)
    add(txt2);
    passtxt1.setEchoCharacter('*');
    add(passtxt1);
    passtxt2.setEchoCharacter('*');
    add(passtxt2);
    add(button1);
    }

    public boolean action(Event evt,Object obj)
    {
    if (evt.target ==txt1)
    {
    txt2.setText(txt1.getText());
    return true;
    }
    if (evt.target == passtxt1)
    {
    passtxt2.setText(passtxt1.getText());
    return true;
    }

    }
    }

    In TextArea we can not use Action method.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice