Reputation: 115
Hello I need some help validing textfield in my gui. My professor gave me a hint on how to do but it freezes up my program. I've tried other methods but if If I enter text the text field the next window doesn't show.
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.*;
//import statements here
public class UserWindow extends JFrame
{
private JTextField nameField, ageField, creditCardField;
private JButton backButton, nextButton;
public UserWindow()
{
super("Please enter your information");
setSize(700,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(4,2));
buildPanel();
setVisible(true);
}
private void buildPanel(){
nameField = new JTextField(10);
ageField = new JTextField(2);
creditCardField = new JTextField(10);
backButton = new JButton("Back");
nextButton = new JButton("Next");
nextButton.addActionListener(new NextButton());
backButton.addActionListener(new BackButton());
JLabel NameLabel = new JLabel("Please enter your name");
JLabel ageLabel = new JLabel("Enter your age");
JLabel creditCardLabel = new JLabel("Enter your credit card number");
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JPanel panel6 = new JPanel();
JPanel panel7 = new JPanel();
JPanel panel8 = new JPanel();
add(panel1);
add(panel2);
add(panel3);
add(panel4);
add(panel5);
add(panel6);
add(panel7);
add(panel8);
panel1.add(NameLabel);
panel2.add(nameField);
panel4.add(ageField);
panel6.add(creditCardField);
panel3.add(ageLabel);
panel5.add(creditCardLabel);
panel7.add(backButton);
panel8.add(nextButton);
}//end of panel building
//action listeners for fields/buttons
private class NextButton implements ActionListener{
public void actionPerformed(ActionEvent e){
String str;
int age;
str = nameField.getText();
if (str.equals("")){
JOptionPane.showMessageDialog(null,"Please enter your name.");
while(true){
nameField.requestFocusInWindow();
if(!str.equals(""))
break;
}
}
if(e.getSource() == nextButton)
new MovieSelection();
setVisible(false);
}
}
private class BackButton implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource() == backButton)
setVisible(false);
new SelectUserWindow();
}
}
}
Upvotes: 0
Views: 34469
Reputation: 7457
It is freezing because you have a this:
if (str.equals("")){
JOptionPane.showMessageDialog(null,"Please enter your name.");
while(true){ //<- Runs forever
nameField.requestFocusInWindow();
if(!str.equals(""))
break;
}
}
AcId is correct when he say you never assign any value to str
after you enter the loop (or the if
). This means you never change the value of str
and it will never not be equal to ""
and it will run in the loop forever. The loop runs in the same thread as the user interface, so the user interface will not have time to do anything else (hence it is freezing).
It doesn't make sense to have a loop that runs forever in this situation. It seems you are trying to make the GUI busy wait until the user has entered a name. Don't do that. Instead check if the user have entered something, if not, alert the user and wait for the user to click the button again.
So remove the loop and use a simple if-else
statement.
if (str.equals("")){ //User have not entered anything.
JOptionPane.showMessageDialog(null,"Please enter your name.");
nameField.requestFocusInWindow();
//Do NOT loop here.
}
else {
//Do everything you need to do when the user have entered something
}
Upvotes: 1
Reputation: 458
You never assign a new value to str
if it is initially the empty string.
I think you need something like
str = nameField.getText();
inside your while(true)
loop.
Upvotes: 2