Swing provides several such components from the most basic single-line text field to the sophisticated multi-line components that understand and support HTML and RTF. Swing also provides a convenient class of entry fields specifically suited for entering passwords.
JTextComponent
javax.swing.JTextComponent
Methods Description String getText ()
Returns the text contained in this TextComponent. String getText (int offs, int len)
Fetches a portion of the text represented by the component. void setText (String t)
Sets the text of this TextComponent to the specified text. void setEditable (boolean b)Sets the specified boolean to indicate whether or not this TextComponent should be editable. void selectAll ()
Selects all the text in the TextComponent. void select (int selectionStart, int selectionEnd)Selects the text between the specified start and end positions.
JTextField
javax.swing.JTextField
JTextFeild is a lightweight componenet that allows the editing of a single line of text.
Constructor Description JTextField ()
Constructs a new TextField
. JTextField (Document doc, String text, int columns)
Constructs a new JTextField
that uses the given text storage model and the given number of columns. JTextField (int columns)
Constructs a new empty TextField
with the specified number of columns. JTextField (String text)
Constructs a new TextField
initialized with the specified text. JTextField (String text, int columns)
Constructs a new TextField
initialized with the specified text and columns.
Example of JTextField
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FullName extends JFrame implements ActionListener
{
JLabel l1,l2;
JTextField txtfn,txtln,txtfull;
JButton b1,b2;
public FullName()
{
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
l1=new JLabel("Enter First name:");
l2=new JLabel("Enter Last name:");
txtfn=new JTextField(20);
txtln=new JTextField(20);
txtfull=new JTextField(50);
txtfull.setEditable(false);
b1=new JButton("Submit");
b2=new JButton("Reset");
b1.addActionListener(this);
b2.addActionListener(this);
add(l1);
add(txtfn);
add(l2);
add(txtln);
add(b1);
add(b2);
add(txtfull);
setTitle("Name Demo");
setBounds(200,300,400,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
String s1=txtfn.getText();
String s2=txtln.getText();
txtfull.setText("Your Name is"+(s1+s2));
}
else
{
txtfn.setText("");
txtln.setText("");
txtfull.setText("");
}
}
public static void main(String[] args)
{
new FullName();
}
}
JTextArea
javax.swing.JTextArea
A JTextArea is a multi-line area that displays plain text. It is intended to be a lightweight component.
Constructors Description JTextArea ()
Constructs a new TextArea. JTextArea (int rows, int cols)
Constructs a new empty TextArea with the specified number of rows and columns. JTextArea (String text)
Constructs a new TextArea with the specified text displayed. JTextArea (String text, int rows, int cols)
Constructs a new TextArea with the specified text and number of rows and columns. JTextField (String text, int cols)
Constructs a new TextField
initialized with the specified text and columns.
Methods Description void append (String str)
Appends the given text to the end of the document. int getColumns ()
Returns the number of columns in the TextArea. void setColumns (int columns)
Sets the number of columns for this TextArea. void insert (String str, int pos)
Inserts the specified text at the specified position. void setFont (Font f)
Sets the current font. int getRows ()
Returns the number of rows in the TextArea. void setRows (int rows)
Sets the number of rows for this TextArea.
JEditorPane
javax.swing.JEditorPane
A JEditorPane is a region that is designed to parse and edit specific types of structured text content. If a scrollbar region is required, this component should be placed inside a JScrollPane object.
Constructors Description JEditorPane ()
Creates a new JEditorPane
. JEditorPane (String url)
Creates a JEditorPane
based on a string containing a URL specification. JEditorPane (String type, String text)
Creates a JEditorPane
that has been initialized to the given text. JEditorPane (URL initialPage)
Creates a JEditorPane
based on a specified URL for input.
JPasswordField
javax.swing.JPasswordField
JPasswordField
is a lightweight component that allows the editing of a single line of text where the view indicates something was typed, but does not show the original characters.
Constructors Description JPasswordField ()
Constructs a new JPasswordField
, with a default document, null
starting text string, and 0 column width. JPasswordField (int columns)
Constructs a new empty JPasswordField
with the specified number of columns. JPasswordField (String text)
Constructs a new JPasswordField
initialized with the specified text.displayed. JPasswordField (String text, int columns)
Constructs a new JPasswordField
initialized with the specified text and columns.
Methods Description char getEchoChar ()
Returns the character to be used for echoing. void setEchoChar (char c)
Sets the echo character for this JPasswordField
. char[]getPassword ()
Returns the text contained in this TextComponent
.
Example for JPasswordFeild
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextDemo extends JFrame implements ActionListener
{
JTextField tnm;
JPasswordField tpwd;
JLabel lnm,lpwd,lmsg;
JButton btnok;
String msg="";
public JTextDemo ()
{
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
tnm=new JTextField(10);
tnm.setToolTipText(“Enter your name”);
tpwd=new JPasswordField(6);
tpwd.setEchoChar('*');
lnm=new JLabel("Name:");
lpwd=new JLabel("Password:");
lmsg=new JLabel("");
btnok=new JButton(" Ok ");
cp.add(lnm);
cp.add(tnm);
cp.add(lpwd);
cp.add(tpwd);
cp.add (btnok);
cp.add (lmsg);
btnok.addActionListener(this);
setTitle("Password Demo");
setBounds(200,300,400,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==btnok)
{
if (tnm.getText().equals("")||tpwd.getText().equals(""))
{
lmsg.setText("plz Enter username/password"); }
else
{
lmsg.setText("Welcome, "+tnm.getText());
}
}
}
public static void main(String[] args)
{
new JTextDemo ();
}
}
JTextPane
javax.swing.JTextPane
A text component that can be marked up with attributes that are represented graphically.
VIDEO