No action under action listener (ActionListener added!)

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

No action under action listener (ActionListener added!)

 
0
  #1
Mar 24th, 2008
I'm trying to show a dialog message if the user clicks a button and a text field is empty.

  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)

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

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

 
-1
  #2
Mar 24th, 2008
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.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

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

 
0
  #3
Mar 24th, 2008
No that's not it...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

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

 
0
  #4
Mar 24th, 2008
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:
  1. if(txtPlayerOneName.getText().equals(emptyText))
Last edited by VernonDozier; Mar 24th, 2008 at 7:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

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

 
0
  #5
Mar 24th, 2008
Sorry I don't understand, message saying program is inside ActionPerformed?

From what I got from that was
  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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

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

 
0
  #6
Mar 24th, 2008
Something like this:
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

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

 
0
  #7
Mar 24th, 2008
Thanks, I got source is cmdSaveSettings so that means theres something wrong with my second if statement?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

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

 
0
  #8
Mar 25th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

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

 
0
  #9
Mar 25th, 2008
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
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC