Sunday, 7 April 2019

JTextField in Java | JTextField in java swing with Example - Online Help

JTextField in Java:  

       The JText component is used to accept text input from the user. There are two classes available in the text component.
-TextField
-TextArea.

     The JTextField class can be used to enter a single line of text. The JTextField in java has four constructors. They are


JTextField()

It is used to create an empty JTextField.

JTextField(String str)

It is used to create a JTextField with the specified string.

JTextField(int numcols)

it is used to create a JTextField with the specified number of columns.

JTextField(String str,int numcols)

It is used to creates a JTextField with the specified text and the specified number of columns.

Methods for JTextField in java:

The most commonly used JTextField() methods are as follows:

void addActionListener(ActionListener Handler)

This method is used to configure an event handler for the JTextField.

String get Text()

The getText() method is used to return the text in the field.

void setEditable(Boolean state)

The setEditable() method is used to set the field as being editable or fixed. If the state is true, the JTextField may be edited. If the state is false, it cannot be edited.

void setText(String str)

The setText()method is used to set the text for the field.

Sample program for JTextField in java:

import java.awt.*;
import javax.swing.*;
/*<applet code="textdemo" width=500 height=500></applet>*/
public class textdemo extends JApplet
{
JTextField jtf=new JTextField(25);
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
c.add(jft);
jft.setText("welcome to online help");
}
}

No comments:

Post a Comment