Why does getText() return an empty string even if the text field is not empty?

59 views Asked by At

For context I am creating a button wherein when player presses it, it gets the text in the text area and does its thing

/**
 * Creates and displays the name input Confirm button
 * @param nameInputActionListener The action to be done when confirm button is pressed
 */
public void createAndDisplay_nameInputConfirmButton(ActionListener nameInputActionListener)
{
    this.nameInputConfirmButton = new JButton("Confirm");
    this.nameInputConfirmButton.addActionListener(nameInputActionListener);
    this.nameInputConfirmButton.setBounds(1250, 340, 100, 50);
    this.characterCreationScreenFrame.add(this.nameInputConfirmButton);
}
this.nameInputArea = new JTextField(40);
this.nameInputArea.setBounds(950, 350, 250, 30);
this.nameInputArea.setToolTipText("Enter Name here!");
this.characterCreationScreenFrame.add(this.nameInputArea);
/**
 * @return The text inputted by player in the name input text area
 */
public String getNameInputtedByPlayer()
{
    return this.nameInputArea.getText();
}

I tried doing the same concept of implementation as taught in a YouTube video and it works, so I don't understand why this wouldn't. The main issue is getText() does not retrieve anything.

here is the action listener of it

/**
 * Creates the action associated with the Name Input button. Moreover, it temporarily hides the character creation buttons.
 */
private void create_ActionFor_NameInputButton()
{
    // NOTE! dinispose NOT HIDE kasi para kapag player goes back to charac screen gagawa ulit new gui para updated yung current charac details
    characterCreationScreenView.hide_CharacterCreationScreenChoicesList();
    characterCreationScreenView.createAndDisplay_NameInput();
    
    // ACTION TO BE DONE KAPAG NAGCONFIRM SI PPLAYER AFTER INPUTTING A NAME 
    ActionListener nameInput_ActionListener = new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent nameInput)
        {        
                // gets the text na nilagay sa text area and checks if it valid (at least > 1) IF OO TRIM NAME IN CASE EXCEEDING 25
                if(characterCreationScreenModel.checkIfNameInputIsValid(characterCreationScreenView.getNameInputtedByPlayer()) == true)
                {                        
                   String validatedName = characterCreationScreenModel.remove_CharactersExceeding25(characterCreationScreenView.getNameInputtedByPlayer());
                   characterCreationScreenModel.setPlayerName(validatedName);
                   characterCreationScreenView.hide_DisplayNameInput();
                   characterCreationScreenView.unhide_CharacterCreationScreenChoicesList();
                   characterCreationScreenView.createAndDisplay_CurrentCharacterDetailsLabels();
                }
                else
                {
                    characterCreationScreenView.display_NameInputErrorMessage(); 
                    System.out.println("" +characterCreationScreenView.getNameInputtedByPlayer().length());

                }
                

            
        }
    };
    //characterCreationScreenView.createAndDisplay_nameInputConfirmButton(nameInput_ActionListener);
    characterCreationScreenView.createAndDisplay_nameInputConfirmButton(nameInput_ActionListener);


} 

here is the example output: enter image description here

0

There are 0 answers