reefcrazed
asked on
Problem adding JTextField to Container
Okay Experts have another java problem.
I have a program that creates textfields that will be filled with user inputs once they press a "New Users" button. This I have managed to accomplish, but I am also supposed to create a field under the buttons that contains the same infomation. The Problem I am having is creating that field and filling it with the data.
Here is the snippet of code from the actionlistener for that button.
public void actionPerformed ( ActionEvent event )
{
if ( event.getSource() == btnNewUser )
{
JPanel centerLayout = new JPanel();
// Prompt User for Input
userName = JOptionPane.showInputDialo g( "Please Enter Your Name." );
userAge = Integer.parseInt(JOptionPa ne.showInp utDialog( "Please Enter Your Age.") );
// Determine User Gender
userSex = JOptionPane.showInputDialo g( "Please Enter you Gender.\nM for Male\nF for Female");
if (userSex == "F")
{
userGender = "Female";
}
else
{
userGender = "Male";
}
userHeight = Integer.parseInt( JOptionPane.showInputDialo g( "Please Enter Your Height" +
"\nIn Inches") );
userWeight = Integer.parseInt( JOptionPane.showInputDialo g( "Please Enter Your Weight" +
"\n In Pounds") );
// Set Text In TextFields
nameField.setText( userName );
ageField.setText( "" + userAge );
genderField.setText( userGender );
weightField.setText( "" + userWeight );
heightField.setText( "" + userHeight );
// Create User Object
User user = new User( userName, userAge, userWeight, userSex, userHeight );
String displayOutput = userName
JTextField display = new JTextField (displayOutput);
centerLayout.add(display);
c.add( centerLayout,BorderLayout. CENTER );
btnOldUser.setEnabled( false );
}
When I run the program and click the "New User" button it gives a null pointer exception at ---c.add( centerLayout,BorderLayout. CENTER );---
I have a program that creates textfields that will be filled with user inputs once they press a "New Users" button. This I have managed to accomplish, but I am also supposed to create a field under the buttons that contains the same infomation. The Problem I am having is creating that field and filling it with the data.
Here is the snippet of code from the actionlistener for that button.
public void actionPerformed ( ActionEvent event )
{
if ( event.getSource() == btnNewUser )
{
JPanel centerLayout = new JPanel();
// Prompt User for Input
userName = JOptionPane.showInputDialo
userAge = Integer.parseInt(JOptionPa
// Determine User Gender
userSex = JOptionPane.showInputDialo
if (userSex == "F")
{
userGender = "Female";
}
else
{
userGender = "Male";
}
userHeight = Integer.parseInt( JOptionPane.showInputDialo
"\nIn Inches") );
userWeight = Integer.parseInt( JOptionPane.showInputDialo
"\n In Pounds") );
// Set Text In TextFields
nameField.setText( userName );
ageField.setText( "" + userAge );
genderField.setText( userGender );
weightField.setText( "" + userWeight );
heightField.setText( "" + userHeight );
// Create User Object
User user = new User( userName, userAge, userWeight, userSex, userHeight );
String displayOutput = userName
JTextField display = new JTextField (displayOutput);
centerLayout.add(display);
c.add( centerLayout,BorderLayout.
btnOldUser.setEnabled( false );
}
When I run the program and click the "New User" button it gives a null pointer exception at ---c.add( centerLayout,BorderLayout.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
(IOW you may not have assigned 'c' - so check it)
ASKER
That did it CEHJ, Thanks a ton!
:-)