Search code examples
javaif-statementcheckboxjtextfield

Make button recognize text in JTextField


My goal here is to make two checkboxes become enabled when someone types the word "Physique" into a certain JTextField.

SO:

Press button
Button reads text in JTextField
if text in field is equal to Physique
then enable Checkboxes Three and Four.

However, I cannot make it respond when text is entered in the JTextField

String greatOne = skillGreat1JTextField.getText();

if ( greatOne.equals( "Physique" ) ) {

        physStress3CheckBox.setEnabled( true );
        physStress4CheckBox.setEnabled( true );

    } 

This triggers when I press an Apply button, but for some reason. This if statement doesn't seem to perform because the Checkboxes never become enabled when Physique is entered into the JTextField

Anyone have a clue why this won't perform? I'm at a loss.

[EDIT]

Here's some more of the code. When I press a button named Apply, this should perform.

public void applyJButtonActionPerformed( ActionEvent event ) {

    String greatOne = skillGreat1JTextField.getText();

    if ( greatOne.equals( "Physique" ) ) {

        physStress3CheckBox.setEnabled( true );
        physStress4CheckBox.setEnabled( true );

    } 
}

That's the IF statement, here's the code for the button:

applyJButton.addActionListener(
        new ActionListener() {
            public void actionPerformed( ActionEvent event ) {
                applyJButtonActionPerformed( event );
            }
        }
    );

Also, in reply to Rudi, Should I be using an OnClickListener? I've been using an ActionListener.

[LAST EDIT]

I've figured it out. It turns out my issue was a conflict with something that happens later in the event, I just took out the line of code that was Turning them back off after it had just turned them on. Thanks everyone for all of your help.

System.out.println("Some text");

This one line of text solved my entire problem. Thanks again everyone! You've saved my FATE games.


Solution

  • OP here. The problem was within the same action.

    I had an IF statement later in the button (That didn't seem important at the time) that referred to a different Checkbox that would Unlock all of the skill buttons:

    if ( !unlockSkillsJCheckBox.isSelected() ) {
            skillSuperb1JTextField.setEditable( false );
            skillSuperb2JTextField.setEditable( false );
            skillSuperb3JTextField.setEditable( false );
            skillSuperb4JTextField.setEditable( false );
            skillSuperb5JTextField.setEditable( false );
            skillGreat2JTextField.setEditable( false );
            skillGreat3JTextField.setEditable( false );
            skillGreat4JTextField.setEditable( false );
            skillGreat5JTextField.setEditable( false );
            skillGood3JTextField.setEditable( false );
            skillGood4JTextField.setEditable( false );
            skillGood5JTextField.setEditable( false );
            skillFair4JTextField.setEditable( false );
            skillFair5JTextField.setEditable( false );
            skillAverage5JTextField.setEditable( false );
    
            consEXTRA2JTextField.setEditable( false );
    
            physStress3CheckBox( false ); //THE PROBLEM LIES HERE.
            physStress4Checkbox( false ); //THE PROBLEM LIES HERE.
    
            mentStress3CheckBox.setEnabled( false );
            mentStress4CheckBox.setEnabled( false );
    
        }
    

    The if statement was running perfectly and functioning as it was supposed to. But because of those two lines, as soon as they turned on, they turned right back off again.

    Although I did learn that System.out.println("Some text"); can be your best friend. Use this to test for logic errors.

    My error was a logic error that happens later in the same action listener I had. Hope this helps anyone having my same problem! Thanks Stack!