zeroaim
zeroaim

Reputation: 13

Retrieving Values From A JTextField

I currently building a hotel reservation system, and I'm having issues with retrieving and setting other values from a JTextField.

What I'm trying to do is to retrieve the value that was inputted into a textfield, and then setting that value to a string in another class.

In here, I'm trying to retrieve values from the JTextField:

@Override
public void actionPerformed(ActionEvent event) {
    GuestInfo gi = new GuestInfo();

    if (event.getSource()==roomView)
    {
        roomViewFrame.setVisible(true);
        roomViewFrame.setSize(1000, 600);

    }

    if (event.getSource()==confirmGuestInfo)
    {
        String firstNameValue = firstNameInput.getText();
        if (firstNameValue.equals("")){
            System.out.printf("Please input first name!");
        }
        else{
            firstNameValue = gi.getFirstName();
        }

        String lastNameValue = lastNameInput.getText();
        if (lastNameValue.equals("")){
            System.out.printf("Please input last name!");
        }
        else{
            lastNameValue = gi.getLastName();
        }

        String addressValue = addressInput.getText();
        if (addressValue.equals("")){
            System.out.printf("Please input address!");
        }
        else{
            addressValue = gi.getAddress();
        }

        String phoneNumberValue = phoneNumberInput.getText();
        if (phoneNumberValue.equals("")){
            System.out.printf("Please input phone number!");
        }
        else{
            phoneNumberValue = gi.getPhoneNumber();
        }

This is my GuestInfo ("gi") class:

import java.util.*;

import javax.swing.JButton;
import javax.swing.*;
public class GuestInfo {

    public static String firstName;
    public static String lastName;
    public static String address;
    public static String phoneNumber;
    public static String numberOfGuests;
    public static String status; //Still checked in or checked out?
    public static String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public static String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public static String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getNumberOfGuests() {
        return numberOfGuests;
    }

    public void setNumberOfGuests(String numberOfGuests) {
        this.numberOfGuests = numberOfGuests;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }





}


/*
 *  JFrame frame = new JFrame();
    JButton button = new JButton("YES");

 * public GuestInfo()

{
frame.setLayout(null);
frame.setSize(600, 600);
frame.setVisible(true);
frame.add(button);
button.setBounds(10, 200,300, 300);

}*/

Upvotes: 1

Views: 87

Answers (2)

Elisio_2GR
Elisio_2GR

Reputation: 1

as MadProgrammer said based on limited info provided, i am assuming that on your actionPerformed method you are getting all guest informations to save on guestInfo class. if thats the case, than i guess what you mean is to use the setter methods not the getters as in:

String firstNameValue = firstNameInput.getText();
if (firstNameValue.equals("")){
    System.out.printf("Please input first name!");
}
else{
    //firstNameValue = gi.getFirstName(); //not this
    gi.setFirstName(firstNameValue); //but this
}

and also all your atributes on guestInfo should be of private scope instead of public since you are providing the getters for them...

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347184

So based on the limited information you've provided, this doesn't make sense...

String firstNameValue = firstNameInput.getText();
if (firstNameValue.equals("")){
    System.out.printf("Please input first name!");
}
else{
    firstNameValue = gi.getFirstName();
}

Basically, you've asked for the value from the firstNameInput field, checked to see if it's blank, but if it's not, you've immediately assigned the value from gi to the value firstNameValue, which based on the usage of GuestInfo gi = new GuestInfo(); would render the values blank anyway...

So, you're only accepting values from the UI when they are blank?

This public static String firstName; is also a really, really, bad idea. What happens when you have more then one guest? They all share the same information?

Upvotes: 1

Related Questions