Thursday, July 8, 2010

Using JTextField and JTextArea  

0 comments

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 usingvoid setText(String txt).
The user can select a portion from the JTextField and JTextArea usingString 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 useboolean 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();
}
}.

What next?

You can also bookmark this post using your favorite bookmarking service:

Related Posts by Categories



Extension Factory Builder