954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

No action under action listener (ActionListener added!)

I'm trying to show a dialog message if the user clicks a button and a text field is empty.

Action listener -

        if(event.getSource() == cmdSaveSettings) 
        if(txtPlayerOneName.getText().equals(emptyText))        
        {
             JOptionPane.showMessageDialog(null, "alert", "Please enter player one's name",                                  JOptionPane.ERROR_MESSAGE);
        }


Ive add an action to listener to the button and the textfield:
(mouse listener also as removes original text on click which works fine)

cmdSaveSettings = new JButton("Save Settings");
        this.cmdSaveSettings.addActionListener(this);

        txtPlayerOneName = new TextField("Player One", 15);
        this.txtPlayerOneName.addMouseListener(this);
        this.txtPlayerOneName.addActionListener(this);
StephNicolaou
Posting Whiz in Training
204 posts since Nov 2007
Reputation Points: 77
Solved Threads: 18
 

It might help if you added brackets to the first if statement. (In red)

Action listener -

        if(event.getSource() == cmdSaveSettings) {
        if(txtPlayerOneName.getText().equals(emptyText))        
        {
             JOptionPane.showMessageDialog(null, "alert", "Please enter player one's name",                                  JOptionPane.ERROR_MESSAGE);
        }
}
jasimp
Senior Poster
3,623 posts since Aug 2007
Reputation Points: 533
Solved Threads: 53
 

No that's not it...

StephNicolaou
Posting Whiz in Training
204 posts since Nov 2007
Reputation Points: 77
Solved Threads: 18
 

I would put in some code at the top of the actionPerformed function that displays a message saying the program is inside actionPerformed. Or you can add a breakpoint and do the same thing. If it doesn't get inside the actionPerformed function, then the problem is not there. If it does, put some code to display that the event source is indeed cmdSaveSettings. If it isn't, again the problem is probably outside of actionPerformed. Then narrow it down some more to see if it gets in the code belonging to this if statement:

if(txtPlayerOneName.getText().equals(emptyText))
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Sorry I don't understand, message saying program is inside ActionPerformed?

From what I got from that was

JOptionPane.showMessageDialog(add.ActionListener.this)

but thats not even the correct syntax...Sorry I'm new to this, could you explain in a bit more detail?

StephNicolaou
Posting Whiz in Training
204 posts since Nov 2007
Reputation Points: 77
Solved Threads: 18
 

Something like this:

public void actionPerformed(ActionEvent event)  
{
     System.out.println ("In actionPerformed");
     if(event.getSource() == cmdSaveSettings) 
     {
            System.out.println ("Source is cmdSaveSettings");
            if(txtPlayerOneName.getText().equals(emptyText))        
            {
                  System.out.println ("Inside if statement");
                  JOptionPane.showMessageDialog(null, "alert", "Please enter player one's name",  
                          JOptionPane.ERROR_MESSAGE);
            }
     }
}

Depending on which, if any, of these statements display as output when you press the button, you have a better handle on what is happening and what the problem is.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Thanks, I got source is cmdSaveSettings so that means theres something wrong with my second if statement?

StephNicolaou
Posting Whiz in Training
204 posts since Nov 2007
Reputation Points: 77
Solved Threads: 18
 

I've solved it, never mind! Thanks anyway - The empty text was set to null so I just changed null to "" and it worked. I wish the java website said that it made a difference!

StephNicolaou
Posting Whiz in Training
204 posts since Nov 2007
Reputation Points: 77
Solved Threads: 18
 

Well, equals compares the String values (the String equals method that is, of course) of the referenced Objects. The String value of the null reference is "null" not "". Keep that in mind. ;-)

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You