943,648 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1697
  • Java RSS
Mar 24th, 2008
0

No action under action listener (ActionListener added!)

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

java Syntax (Toggle Plain Text)
  1. Action listener -
  2.  
  3. if(event.getSource() == cmdSaveSettings)
  4. if(txtPlayerOneName.getText().equals(emptyText))
  5. {
  6. JOptionPane.showMessageDialog(null, "alert", "Please enter player one's name", JOptionPane.ERROR_MESSAGE);
  7. }

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

java Syntax (Toggle Plain Text)
  1. cmdSaveSettings = new JButton("Save Settings");
  2. this.cmdSaveSettings.addActionListener(this);
  3.  
  4. txtPlayerOneName = new TextField("Player One", 15);
  5. this.txtPlayerOneName.addMouseListener(this);
  6. this.txtPlayerOneName.addActionListener(this);
Last edited by WolfPack; Mar 25th, 2008 at 11:22 am. Reason: corrected code tags.
Similar Threads
Reputation Points: 39
Solved Threads: 12
Junior Poster
StephNicolaou is offline Offline
156 posts
since Nov 2007
Mar 24th, 2008
0

Re: No action under action listener (ActionListener added!)

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);
        }
}
Last edited by jasimp; Mar 24th, 2008 at 6:11 pm.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Mar 24th, 2008
0

Re: No action under action listener (ActionListener added!)

No that's not it...
Reputation Points: 39
Solved Threads: 12
Junior Poster
StephNicolaou is offline Offline
156 posts
since Nov 2007
Mar 24th, 2008
0

Re: No action under action listener (ActionListener added!)

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:
Java Syntax (Toggle Plain Text)
  1. if(txtPlayerOneName.getText().equals(emptyText))
Last edited by VernonDozier; Mar 24th, 2008 at 7:19 pm.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Mar 24th, 2008
0

Re: No action under action listener (ActionListener added!)

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

From what I got from that was
Java Syntax (Toggle Plain Text)
  1. 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?
Reputation Points: 39
Solved Threads: 12
Junior Poster
StephNicolaou is offline Offline
156 posts
since Nov 2007
Mar 24th, 2008
0

Re: No action under action listener (ActionListener added!)

Something like this:
JAVA Syntax (Toggle Plain Text)
  1. public void actionPerformed(ActionEvent event)
  2. {
  3. System.out.println ("In actionPerformed");
  4. if(event.getSource() == cmdSaveSettings)
  5. {
  6. System.out.println ("Source is cmdSaveSettings");
  7. if(txtPlayerOneName.getText().equals(emptyText))
  8. {
  9. System.out.println ("Inside if statement");
  10. JOptionPane.showMessageDialog(null, "alert", "Please enter player one's name",
  11. JOptionPane.ERROR_MESSAGE);
  12. }
  13. }
  14. }
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Mar 24th, 2008
0

Re: No action under action listener (ActionListener added!)

Thanks, I got source is cmdSaveSettings so that means theres something wrong with my second if statement?
Reputation Points: 39
Solved Threads: 12
Junior Poster
StephNicolaou is offline Offline
156 posts
since Nov 2007
Mar 25th, 2008
0

Re: No action under action listener (ActionListener added!)

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!
Reputation Points: 39
Solved Threads: 12
Junior Poster
StephNicolaou is offline Offline
156 posts
since Nov 2007
Mar 25th, 2008
0

Re: No action under action listener (ActionListener added!)

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. ;-)
Last edited by masijade; Mar 25th, 2008 at 9:52 am. Reason: Added parenthesised phrase
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: 55: ')' expected (Error cant compile) PLZ HELP!
Next Thread in Java Forum Timeline: java problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC