How to make cursor auto tab to next textfield?

T

tianyiny

I'm a intro Java programmer, my professor asking to do the input for
SS# in 3 textfiled.

Even it just a optional question, but I'd like to know how to apply
this function in my code.

SS# should be like 123-45-6789, when I type in 123 in the first
textfield, how to make the cursor auto jump

to the next textfield for me to enter 45 and then jump to the next for
6789?

And my code is this:


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

public class HW2 extends JFrame {
// This next has to do with serialization, a topic
// not covered in this course. Including this line
// avoids an annoying warning message.
boolean firstNameRequiredChangeColor = false;
boolean middleInitialNameRequiredChangeColor = false;
boolean lastNameRequiredChangeColor = false;
boolean street1RequiredChangeColor = false;
boolean street2RequiredChangeColor = false;
boolean cityRequiredChangeColor = false;
boolean stateRequiredChangeColor = false;
boolean zipCodeRequiredChangeColor = false;
boolean phoneNumberRequiredChangeColor = false;
boolean socialSecurityNumberRequiredChangeColor = false;
boolean socialSecurity2NumberRequiredChangeColor = false;
boolean socialSecurity3NumberRequiredChangeColor = false;

private static final long serialVersionUID = 1L;
private String firstNameAsEntered = new String();
private String middleInitialNameAsEntered = new String();
private String lastNameAsEntered = new String();
private String street1AsEntered = new String();
private String street2AsEntered = new String();
private String cityAsEntered = new String();
private String stateAsEntered = new String();
private String zipCodeAsEntered = new String();
private String phoneNumberAsEntered = new String();
private String socialSecurityNumberAsEntered = new String();
private String socialSecurity2NumberAsEntered = new String();
private String socialSecurity3NumberAsEntered = new String();

private javax.swing.JPanel jContentPane = null;
private JLabel instructions = null;
private JLabel firstNameLabel = null;
private JLabel middleInitialLabel = null;
private JLabel lastNameLabel = null;
private JLabel street1Label = null;
private JLabel street2Label = null;
private JLabel cityLabel = null;
private JLabel stateLabel = null;
private JLabel zipCodeLabel = null;
private JLabel phoneNumberLabel = null;
private JLabel socialSecurityNumberLabel = null;

private JTextField firstNameEntry = null;
private JTextField middleInitialEntry = null;
private JTextField lastNameEntry = null;
private JTextField street1Entry = null;
private JTextField street2Entry = null;
private JTextField cityEntry = null;
private JTextField stateEntry = null;
private JTextField zipCodeEntry = null;
private JTextField phoneNumberEntry = null;
private JTextField socialSecurityNumberEntry = null;
private JTextField socialSecurity2NumberEntry = null;
private JTextField socialSecurity3NumberEntry = null;

private JButton submitJButton = null;
public static void main(String[] args) {
HW2 frame = new HW2();
frame.setVisible(true);
} // end main

// constructor
public HW2() {
// Invoke constructor of superclass.
// Would be supplied by compiler, _in this case_.
super();
initialize();
} // end HW1StarterKit constructor

// "this" is always a reference to the object currently executing.
// (Get used to it; you'll see it a lot.)

private void initialize() {
this.setSize(605, 280);
this.setContentPane(getJContentPane());
this.setTitle("Customer Data Entry");
}

private JPanel getJContentPane() {
jContentPane = new JPanel();
// Flow layout not really ideal; we'll work on this.
// Simplest way here.
jContentPane.setLayout(new java.awt.FlowLayout());
// We now have a "pane" to hold our components.
// We add them in the order we want them to appear.
jContentPane.add(getInstructions(), null);
jContentPane.add(getFirstNameLabel(), null);
jContentPane.add(getFirstNameEntry(), null);
jContentPane.add(getMiddleInitialLabel(), null);
jContentPane.add(getMiddleInitialEntry(), null);
jContentPane.add(getLastNameLabel(), null);
jContentPane.add(getLastNameEntry(), null);
jContentPane.add(getStreet1Label(), null);
jContentPane.add(getStreet1Entry(), null);
jContentPane.add(getStreet2Label(), null);
jContentPane.add(getStreet2Entry(), null);
jContentPane.add(getCityLabel(), null);
jContentPane.add(getCityEntry(), null);
jContentPane.add(getStateLabel(), null);
jContentPane.add(getStateEntry(), null);
jContentPane.add(getZipCodeLabel(), null);
jContentPane.add(getZipCodeEntry(), null);
jContentPane.add(getPhoneNumberLabel(), null);
jContentPane.add(getPhoneNumberEntry(), null);
jContentPane.add(getSocialSecurityNumberLabel(), null);
jContentPane.add(getSocialSecurityNumberEntry(), null);
jContentPane.add(getSocialSecurity2NumberEntry(), null);
jContentPane.add(getSocialSecurity3NumberEntry(), null);
jContentPane.add(getSubmitButton(), null);

return jContentPane;
}

private JLabel getInstructions() {
instructions = new JLabel();
instructions.setText("* Denotes required field. ");
instructions.setPreferredSize(new java.awt.Dimension(600, 40));
instructions.setHorizontalAlignment(SwingConstants.CENTER);
return instructions;
}
private JButton getSubmitButton() {

if (submitJButton == null) {
submitJButton = new JButton("Submit");

submitJButton.setBounds(new Rectangle(10, 5, 59, 28));
submitJButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

firstNameAsEntered = firstNameEntry.getText().trim();
firstNameEntry.setText(firstNameAsEntered);
middleInitialNameAsEntered = middleInitialEntry.getText().trim();
middleInitialEntry.setText(middleInitialNameAsEntered);
lastNameAsEntered = lastNameEntry.getText().trim();
lastNameEntry.setText(lastNameAsEntered);
street1AsEntered = street1Entry.getText().trim();
street1Entry.setText(street1AsEntered);
street2AsEntered = street2Entry.getText().trim();
street2Entry.setText(street2AsEntered);
cityAsEntered = cityEntry.getText().trim();
cityEntry.setText(cityAsEntered);
stateAsEntered = stateEntry.getText().trim();
stateEntry.setText(stateAsEntered);
zipCodeAsEntered = zipCodeEntry.getText().trim();
zipCodeEntry.setText(zipCodeAsEntered);
phoneNumberAsEntered = phoneNumberEntry.getText().trim();
phoneNumberEntry.setText(phoneNumberAsEntered);
socialSecurityNumberAsEntered =
socialSecurityNumberEntry.getText().trim();
socialSecurityNumberEntry.setText(socialSecurityNumberAsEntered);
socialSecurity2NumberAsEntered =
socialSecurity2NumberEntry.getText().trim();
socialSecurity2NumberEntry.setText(socialSecurity2NumberAsEntered);
socialSecurity3NumberAsEntered =
socialSecurity3NumberEntry.getText().trim();
socialSecurity3NumberEntry.setText(socialSecurity3NumberAsEntered);

if (firstNameAsEntered.length() == 0)
System.out.println("First name is required");
else if(!(firstNameAsEntered.matches("[A-Z][a-zA-Z]*"))) {
firstNameEntry.setForeground(Color.red);
firstNameRequiredChangeColor = true;
System.out.println("Invalid first name");
}
else if (firstNameRequiredChangeColor) {
firstNameRequiredChangeColor = false;
firstNameEntry.setForeground(Color.black);
}
else if (middleInitialNameAsEntered.length() > 1) {
middleInitialEntry.setForeground(Color.red);
middleInitialNameRequiredChangeColor = true;
System.out.println("Middle Initial must be one letter");
}
else if(middleInitialNameRequiredChangeColor) {
middleInitialNameRequiredChangeColor = false;
middleInitialEntry.setForeground(Color.black);
}
else if (middleInitialNameAsEntered.matches ("\\d")) {
middleInitialEntry.setForeground(Color.red);
middleInitialNameRequiredChangeColor = true;
System.out.println("Middle Initial must be a letter");
}
else if(middleInitialNameRequiredChangeColor) {
middleInitialNameRequiredChangeColor = false;
middleInitialEntry.setForeground(Color.black);
}
else if (middleInitialNameAsEntered.matches ("\\W")) {
middleInitialEntry.setForeground(Color.red);
middleInitialNameRequiredChangeColor = true;
System.out.println("Middle Initial must be a letter");
}
else if(middleInitialNameRequiredChangeColor) {
middleInitialNameRequiredChangeColor = false;
middleInitialEntry.setForeground(Color.black);
}
else if (lastNameAsEntered.length() == 0)
System.out.println("Last name is required");
else if (!(lastNameAsEntered.matches("[a-zA-Z]+([ '-][a-zA-Z]
+)*"))) {
lastNameEntry.setForeground(Color.red);
lastNameRequiredChangeColor = true;
System.out.println("Invalid last name");
}
else if(lastNameRequiredChangeColor) {
lastNameRequiredChangeColor = false;
lastNameEntry.setForeground(Color.black);
}
else if (street1AsEntered.length() == 0)
System.out.println("Street 1 is required");
else if (cityAsEntered.length() == 0)
System.out.println("City name is required");
else if (!(cityAsEntered.matches("([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]
+|" +
"[a-zA-Z]+\\s[a-zA-Z]+\\s[a-zA-Z]+|" +
"[a-zA-Z]+\\s[a-zA-Z]+\\s[a-zA-Z]+\\s[a-zA-Z]+)"))) {
cityEntry.setForeground(Color.red);
cityRequiredChangeColor = true;
System.out.println("Invalid city name");
}
else if(cityRequiredChangeColor) {
cityRequiredChangeColor = false;
cityEntry.setForeground(Color.black);
}
else if(stateAsEntered.length()==0)
System.out.println("State name is required");
else if(!(stateAsEntered.matches("([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]
+)"))) {
stateEntry.setForeground(Color.red);
stateRequiredChangeColor = true;
System.out.println("Invalid state name");
}
else if(!stateAsEntered.matches("\\w{2}")){
stateEntry.setForeground(Color.red);
stateRequiredChangeColor = true;
System.out.println("State name must be 2 characters");
}
else if(stateRequiredChangeColor) {
stateRequiredChangeColor = false;
stateEntry.setForeground(Color.black);
}
else if (zipCodeAsEntered.length() == 0)
System.out.println("Zip code is required");
else if (!zipCodeAsEntered.matches("\\d{5}")) {
zipCodeEntry.setForeground(Color.red);
zipCodeRequiredChangeColor = true;
System.out.println("ZIP must be five digits");
}
else if(zipCodeRequiredChangeColor) {
zipCodeRequiredChangeColor = false;
zipCodeEntry.setForeground(Color.black);
}
else if (phoneNumberAsEntered.length() == 0)
System.out.println("Phone number is required");
else if (!(phoneNumberAsEntered.matches("([2-9]\\d{2}-[1-9]\\d{2}-\
\d{4})|([2-9]\\d{9})"))) {
phoneNumberEntry.setForeground(Color.red);
phoneNumberRequiredChangeColor = true;
System.out.println("Invalid phone number");
}
else if(phoneNumberRequiredChangeColor) {
phoneNumberRequiredChangeColor = false;
phoneNumberEntry.setForeground(Color.black);
}
else if (socialSecurityNumberAsEntered.length() == 0)
System.out.println("Social Security number is required");
else if (!(socialSecurityNumberAsEntered.matches("\\d{3}"))) {
socialSecurityNumberEntry.setForeground(Color.red);
socialSecurityNumberRequiredChangeColor = true;
System.out.println("Invalid Social Security Number");
}
else if(socialSecurityNumberRequiredChangeColor) {
socialSecurityNumberRequiredChangeColor = false;
socialSecurityNumberEntry.setForeground(Color.black);
}
else if (socialSecurity2NumberAsEntered.length() == 0)
System.out.println("Social Security number is required");
else if (!(socialSecurity2NumberAsEntered.matches("\\d{2}"))) {
socialSecurity2NumberEntry.setForeground(Color.red);
socialSecurity2NumberRequiredChangeColor = true;
System.out.println("Invalid Social Security Number");
}
else if(socialSecurity2NumberRequiredChangeColor) {
socialSecurity2NumberRequiredChangeColor = false;
socialSecurity2NumberEntry.setForeground(Color.black);
}
else if (socialSecurity3NumberAsEntered.length() == 0)
System.out.println("Social Security number is required");
else if (!(socialSecurity3NumberAsEntered.matches("\\d{4}"))) {
socialSecurity3NumberEntry.setForeground(Color.red);
socialSecurity3NumberRequiredChangeColor = true;
System.out.println("Invalid Social Security Number");
}
else if(socialSecurity3NumberRequiredChangeColor) {
socialSecurity3NumberRequiredChangeColor = false;
socialSecurity3NumberEntry.setForeground(Color.black);
}

else {
String str = e.getActionCommand();
if(str.equals("SUBMIT"))
JOptionPane.showMessageDialog(null, "Thank you.");

System.out.print(firstNameAsEntered + " ");
if (middleInitialNameAsEntered.length()!=0)
System.out.print(middleInitialNameAsEntered.toUpperCase() + ".
");
System.out.println(lastNameAsEntered);
System.out.println(socialSecurityNumberAsEntered + "-" +
socialSecurity2NumberAsEntered + "-" +
socialSecurity3NumberAsEntered);
System.out.print(street1AsEntered + " ");
if (street2AsEntered.length() != 0)
System.out.println(street2AsEntered);
else
System.out.println();
System.out.println(cityAsEntered + ", " + stateAsEntered + " "
+ zipCodeAsEntered);
System.out.println(phoneNumberAsEntered);
}
}
});

}
return submitJButton;
}
private JLabel getFirstNameLabel() {
firstNameLabel = new JLabel();
firstNameLabel.setText("First Name *");
firstNameLabel.setPreferredSize(new Dimension(70, 20));
return firstNameLabel;
} // end private JLabel getFirstNameLabel()

private JTextField getFirstNameEntry() {
firstNameEntry = new JTextField();
firstNameEntry.setPreferredSize(new Dimension(160, 20));
// NOW WATCH closely! Anonymous inner class coming up!
// What is this next thing??? Can you find the closing right paren??
firstNameEntry.addActionListener(
// This is an inner class declaration. But no name: anonymous
// Don't worry about this. We'll be back to it.
new ActionListener() {
public void actionPerformed(ActionEvent e) {

middleInitialEntry.requestFocus();
}
});
return firstNameEntry;
} // end private JTextField getFirstNameEntry()

private JLabel getMiddleInitialLabel() {
middleInitialLabel = new JLabel();
middleInitialLabel.setText("MI");
middleInitialLabel.setPreferredSize(new Dimension(13, 20));
return middleInitialLabel;
} // end private JLabel getMiddleInitialLabel()

private JTextField getMiddleInitialEntry() {
middleInitialEntry = new JTextField();
middleInitialEntry.setPreferredSize(new Dimension(30, 20));
middleInitialEntry.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

lastNameEntry.requestFocus();
}
});
return middleInitialEntry;
} // end private JTextField getMiddleInitialEntry()

private JLabel getLastNameLabel() {
lastNameLabel = new JLabel();
lastNameLabel.setText("Last Name *");
lastNameLabel.setPreferredSize(new Dimension(70, 20));
return lastNameLabel;
} // end private JLabel getLastNameLabel()

private JTextField getLastNameEntry() {
lastNameEntry = new JTextField();
lastNameEntry.setPreferredSize(new Dimension(160, 20));
lastNameEntry.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

street1Entry.requestFocus();
}
});
return lastNameEntry;
} // end private JTextField getLastNameEntry()

private JLabel getStreet1Label() {
street1Label = new JLabel();
street1Label.setText("Street 1 *");
street1Label.setPreferredSize(new Dimension(67, 20));
return street1Label;
}

private JTextField getStreet1Entry() {
street1Entry = new JTextField();
street1Entry.setPreferredSize(new Dimension(480, 20));
street1Entry.addActionListener(

new ActionListener() {
public void actionPerformed(ActionEvent e) {

street2Entry.requestFocus();
}
});
return street1Entry;
}

private JLabel getStreet2Label() {
street2Label = new JLabel();
street2Label.setText("Street 2");
street2Label.setPreferredSize(new Dimension(67, 20));
return street2Label;
}

private JTextField getStreet2Entry() {
street2Entry = new JTextField();
street2Entry.setPreferredSize(new Dimension(480, 20));
street2Entry.addActionListener(

new ActionListener() {
public void actionPerformed(ActionEvent e) {

cityEntry.requestFocus();
}
});
return street2Entry;
}

private JLabel getCityLabel() {
cityLabel = new JLabel();
cityLabel.setText("City *");
cityLabel.setPreferredSize(new Dimension(40, 20));
return cityLabel;
}

private JTextField getCityEntry() {
cityEntry = new JTextField();
cityEntry.setPreferredSize(new Dimension(90, 20));
cityEntry.addActionListener(

new ActionListener() {
public void actionPerformed(ActionEvent e) {

stateEntry.requestFocus();
}
});
return cityEntry;
}

private JLabel getStateLabel() {
stateLabel = new JLabel();
stateLabel.setText("State *");
stateLabel.setPreferredSize(new Dimension(50, 20));
return stateLabel;
}

private JTextField getStateEntry() {
stateEntry = new JTextField();
stateEntry.setPreferredSize(new Dimension(30, 20));
stateEntry.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

zipCodeEntry.requestFocus();
}
});
return stateEntry;
}

private JLabel getZipCodeLabel() {
zipCodeLabel = new JLabel();
zipCodeLabel.setText("ZIP *");
zipCodeLabel.setPreferredSize(new Dimension(40, 20));
return zipCodeLabel;

} // end private JLabel getzipCodeLabel()
private JTextField getZipCodeEntry() {
zipCodeEntry = new JTextField();
zipCodeEntry.setPreferredSize(new Dimension(70, 20));
zipCodeEntry.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

phoneNumberEntry.requestFocus();
}
});
return zipCodeEntry;
} // end private JTextField getLastNameEntry()

private JLabel getPhoneNumberLabel() {
phoneNumberLabel = new JLabel();
phoneNumberLabel.setText("Phone Number *");
phoneNumberLabel.setPreferredSize(new Dimension(100, 20));
return phoneNumberLabel;
}

private JTextField getPhoneNumberEntry() {
phoneNumberEntry = new JTextField();
phoneNumberEntry.setPreferredSize(new Dimension(100, 20));
phoneNumberEntry.addActionListener(

new ActionListener() {
public void actionPerformed(ActionEvent e) {

socialSecurityNumberEntry.requestFocus();
}
});
return phoneNumberEntry;
} // end private JTextField getphoneNumberEntry()

private JLabel getSocialSecurityNumberLabel() {
socialSecurityNumberLabel = new JLabel();
socialSecurityNumberLabel.setText("Social Security Number *");
socialSecurityNumberLabel.setPreferredSize(new Dimension(145,
20));
return socialSecurityNumberLabel;
}

private JTextField getSocialSecurityNumberEntry() {
socialSecurityNumberEntry = new JTextField();
socialSecurityNumberEntry.setPreferredSize(new Dimension(25, 20));
socialSecurityNumberEntry.addActionListener(

new ActionListener() {
public void actionPerformed(ActionEvent e) {


socialSecurity2NumberEntry.requestFocus();
}
});
return socialSecurityNumberEntry;
}

private JTextField getSocialSecurity2NumberEntry() {
socialSecurity2NumberEntry = new JTextField();
socialSecurity2NumberEntry.setPreferredSize(new Dimension(20, 20));
socialSecurity2NumberEntry.addActionListener(

new ActionListener() {
public void actionPerformed(ActionEvent e) {

socialSecurity3NumberEntry.requestFocus();
}
});
return socialSecurity2NumberEntry;
}
private JTextField getSocialSecurity3NumberEntry() {
socialSecurity3NumberEntry = new JTextField();
socialSecurity3NumberEntry.setPreferredSize(new Dimension(35, 20));

return socialSecurity3NumberEntry;
}

} // end class HW2StarterKit
 
B

Brandon McCombs

I'm a intro Java programmer, my professor asking to do the input for
SS# in 3 textfiled.

Even it just a optional question, but I'd like to know how to apply
this function in my code.

SS# should be like 123-45-6789, when I type in 123 in the first
textfield, how to make the cursor auto jump

to the next textfield for me to enter 45 and then jump to the next for
6789?

And my code is this:
[snip]

I'd add a DocumentListener to the first 2 textfields of the SS# and in
the listener check for how many characters are in the first field. When
it detects there are 3 then set the cursor for the 2nd field to position
0 (you may have to have the 2nd field request focus too). Of course for
the 2nd field monitor for 2 characters and then set the cursor in the
3rd field. I haven't yet figured out how to use the same listener for
multiple fields if I need to know which textfield is currently being
changed so I create an anonymous inner class for each JTextField that
needs a documentListener. In that way each JTextField has its own listener.

A DocumentListener requires the following methods to be defined:
public void changedUpdate(DocumentEvent docevt) {}
public void insertUpdate(DocumentEvent docevt) {}
public void removeUpdate(DocumentEvent docevt) {}

I haven't ever tried this so I don't know if it would work.

By the way, to add a document listener to a textfield you have to call
textfield.getDocument() before calling addDocumentListener()

hope that helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,730
Latest member
AudryNolan

Latest Threads

Top