Thursday, April 30, 2009

Using JTextField and JTextArea

TextFields are used to take input from user also it can be used to display results.
The various constructors and methods are defined in the previous JTextField tutorials.

Values from a TextField is extracted using String getText() and set using void setText(String txt).
The user can select a portion from the JTextField and JTextArea using String getSelectedText() and void select(int start_index,int end_index)
String getSelectedText() retrurns the String portion selected by the user from the textfield.
You can also enable or disable the TextField that is if you want TextField to be editable the use the methos void setEditable(boolean value) to true else set false.If you want to get value of editable TextField use boolean isEditable().

Sometimes you wish to set a password field in which the entered character must not be shown.It has to be replaced by some other sign or value.To set a password field you must use void setEchoChar(Char ch) character you specified in 'ch' will be shown when some charater is typed.
If you want to get the Charcter specified the use char getEchoChar().
Example :-
getting selected value from a text field and displaying it to other and use of Password field.

// textfield.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class textfield extends JFrame implements ActionListener
{
JFrame frame;
JPanel panel;
JTextField txtname,txtdisplay;
TextField txtpass;
JButton button;
public textfield()
{
frame=new JFrame("Text Field usage demo");
panel=new JPanel();
txtname=new JTextField(25);
txtpass=new TextField(25);
txtpass.setEchoChar('#');
txtdisplay =new JTextField(25);
JButton button=new JButton("Display selected");
button.addActionListener(this);
panel.setLayout(new GridLayout(3,2));
panel.add(new JLabel("Name :"));
panel.add(txtname);
panel.add(new JLabel("Password :"));
panel.add(txtpass);
panel.add(txtdisplay);
panel.add(button);
frame.getContentPane().add(panel);
frame.show();
frame.pack();

}

public void actionPerformed(ActionEvent ae)
{

txtdisplay.setText("Name :"+txtname.getSelectedText()+" Pass :"+txtpass.getText());

}
public static void main(String a[])
{
new textfield();
}
}

Example:-
getting selected value from a text area and displaying selected in textfield.

// textarea.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class textarea extends JFrame implements ActionListener
{
JFrame frame;
JPanel panel;
JTextField txtname,txtdisplay;
TextField txtpass;
JTextArea area;
JButton button;
public textarea()
{
frame=new JFrame("Text Area usage demo");
panel=new JPanel();
txtdisplay =new JTextField(25);
area = new JTextArea("vaibhav pandey has made this programme",5,5);
JButton button=new JButton("Display selected");
button.addActionListener(this);
panel.add(area);
panel.add(txtdisplay);
panel.add(button);

frame.getContentPane().add(panel);
frame.show();
frame.pack();

}

public void actionPerformed(ActionEvent ae)
{
txtdisplay.setText("Selected String :"+area.getSelectedText());
}
public static void main(String a[])
{
new textarea();
}
}.

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP