AmyS
asked on
KeyListener
I know TextFields can travel by "Tab". How can we make all TextFields traversable by pressing "Enter"?
Could you please give me an example code?
Could you please give me an example code?
ASKER
tarlton,
Your code is good for a couple of TextFields. But I want to search for a better way which
can easily handle more than 20 TextFields traversable by pressing "Enter".
Your code is good for a couple of TextFields. But I want to search for a better way which
can easily handle more than 20 TextFields traversable by pressing "Enter".
When enter key is pressed, the ActionEvent is generated. In the action listener's actionPerformed method, put a one liner. The code is below
public class TextAction implements ActionListener {
public void actionPerformed(ActionEven t e)
{
e.getSource().transferFocu s(); // moves to next field
}
in your code attach action listener as follows
TextAction ta = new TextAction();
for each text field tf add,
tf.addActionListener(ta);
Second method :
Extend TextField class
public class MyTextField extends TextField Implements ActionListener {
public MyTextField (String text) {
super(text);
addActionListener(this);
}
public MyTextField () {
super();
addActionListener(this);
}
public void actionPerformed(ActionEven t e)
{
e.getSource().transferFocu s(); // moves to next field
}
}
public class TextAction implements ActionListener {
public void actionPerformed(ActionEven
{
e.getSource().transferFocu
}
in your code attach action listener as follows
TextAction ta = new TextAction();
for each text field tf add,
tf.addActionListener(ta);
Second method :
Extend TextField class
public class MyTextField extends TextField Implements ActionListener {
public MyTextField (String text) {
super(text);
addActionListener(this);
}
public MyTextField () {
super();
addActionListener(this);
}
public void actionPerformed(ActionEven
{
e.getSource().transferFocu
}
}
ASKER
Hi Evijay,
When I wrote a small code to test your answer, I got an error message:
Method transferFocus() not found in class java.lang.Object.
e.getSource().transferFocu s();
Here is the test code:
import java.awt.*;
import java.awt.event.*;
public class Test1 extends Panel
{
myTextField first, last, company, address, home, work;
public Test1()
{
setBackground(Color.lightG ray);
setLayout(new GridLayout(7,2,5,5));
add(new Label("First Name"));
first = new myTextField("first_name");
add(first);
add(new Label("Last Name"));
last = new myTextField("last_name");
add(last);
add(new Label("Company Name"));
company = new myTextField("company_name" );
add(company);
add(new Label("Address"));
address = new myTextField("726 Forest Dr.");
add(address);
add(new Label("Home Phone #"));
home = new myTextField("home_phone");
add(home);
add(new Label("Work Phone #"));
work = new myTextField("work_phone");
add(work);
}
public Dimension getPreferredSize()
{
return new Dimension(300, 200);
}
public static void main(String s[])
{
Frame frame = new Frame("Focus Example");
Test1 panel = new Test1();
frame.add(panel,"Center");
frame.setSize(panel.getPre ferredSize ());
frame.setVisible(true);
}
public class myTextField extends TextField implements ActionListener {
public myTextField (String text) {
super(text);
addActionListener(this);
}
public myTextField () {
super();
addActionListener(this);
}
public void actionPerformed(ActionEven t e)
{
e.getSource().transferFocu s(); // moves to next field
}
}
}
When I wrote a small code to test your answer, I got an error message:
Method transferFocus() not found in class java.lang.Object.
e.getSource().transferFocu
Here is the test code:
import java.awt.*;
import java.awt.event.*;
public class Test1 extends Panel
{
myTextField first, last, company, address, home, work;
public Test1()
{
setBackground(Color.lightG
setLayout(new GridLayout(7,2,5,5));
add(new Label("First Name"));
first = new myTextField("first_name");
add(first);
add(new Label("Last Name"));
last = new myTextField("last_name");
add(last);
add(new Label("Company Name"));
company = new myTextField("company_name"
add(company);
add(new Label("Address"));
address = new myTextField("726 Forest Dr.");
add(address);
add(new Label("Home Phone #"));
home = new myTextField("home_phone");
add(home);
add(new Label("Work Phone #"));
work = new myTextField("work_phone");
add(work);
}
public Dimension getPreferredSize()
{
return new Dimension(300, 200);
}
public static void main(String s[])
{
Frame frame = new Frame("Focus Example");
Test1 panel = new Test1();
frame.add(panel,"Center");
frame.setSize(panel.getPre
frame.setVisible(true);
}
public class myTextField extends TextField implements ActionListener {
public myTextField (String text) {
super(text);
addActionListener(this);
}
public myTextField () {
super();
addActionListener(this);
}
public void actionPerformed(ActionEven
{
e.getSource().transferFocu
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is working code :
import java.awt.*;
import java.awt.event.*;
public class Test1 extends Panel
{
myTextField first, last, company, address, home, work;
public Test1()
{
setBackground(Color.lightG ray);
setLayout(new GridLayout(7,2,5,5));
add(new Label("First Name"));
first = new myTextField("first_name");
add(first);
add(new Label("Last Name"));
last = new myTextField("last_name");
add(last);
add(new Label("Company Name"));
company = new myTextField("company_name" );
add(company);
add(new Label("Address"));
address = new myTextField("726 Forest Dr.");
add(address);
add(new Label("Home Phone #"));
home = new myTextField("home_phone");
add(home);
add(new Label("Work Phone #"));
work = new myTextField("work_phone");
add(work);
}
public Dimension getPreferredSize()
{
return new Dimension(300, 200);
}
public static void main(String s[])
{
Frame frame = new Frame("Focus Example");
Test1 panel = new Test1();
frame.add(panel,"Center");
frame.setSize(panel.getPre ferredSize ());
frame.setVisible(true);
}
public class myTextField extends TextField implements ActionListener {
public myTextField (String text) {
super(text);
addActionListener(this);
}
public myTextField () {
super();
addActionListener(this);
}
public void actionPerformed(ActionEven t e)
{
((Component)e.getSource()) .transferF ocus(); // moves to next field
}
}
}
import java.awt.*;
import java.awt.event.*;
public class Test1 extends Panel
{
myTextField first, last, company, address, home, work;
public Test1()
{
setBackground(Color.lightG
setLayout(new GridLayout(7,2,5,5));
add(new Label("First Name"));
first = new myTextField("first_name");
add(first);
add(new Label("Last Name"));
last = new myTextField("last_name");
add(last);
add(new Label("Company Name"));
company = new myTextField("company_name"
add(company);
add(new Label("Address"));
address = new myTextField("726 Forest Dr.");
add(address);
add(new Label("Home Phone #"));
home = new myTextField("home_phone");
add(home);
add(new Label("Work Phone #"));
work = new myTextField("work_phone");
add(work);
}
public Dimension getPreferredSize()
{
return new Dimension(300, 200);
}
public static void main(String s[])
{
Frame frame = new Frame("Focus Example");
Test1 panel = new Test1();
frame.add(panel,"Center");
frame.setSize(panel.getPre
frame.setVisible(true);
}
public class myTextField extends TextField implements ActionListener {
public myTextField (String text) {
super(text);
addActionListener(this);
}
public myTextField () {
super();
addActionListener(this);
}
public void actionPerformed(ActionEven
{
((Component)e.getSource())
}
}
}
JTextField m_textField;
JTextField m_secondTextField;
JTextField m_thirdTextField;
m_textField.addKeyListner(
m_secondTextField.addKeyLi
m_thirdTextField.addKeyLis
/**
* The CHandleEnterPress class controls the focus when the Enter key is
* pressed.
**/
class CHandleEnterPress extends KeyAdapter
{
/**
* called when any key is pressed
**/
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
if (e.getSource() == m_textField)
{
m_secondTextField.requestF
}
else if (e.getSource() == m_secondTextField)
{
m_thirdTextField.requestFo
}
}
}
}