| | |
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:
Solved Threads: 0
I'm trying to show a dialog message if the user clicks a button and a text field is empty.
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)
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)
java Syntax (Toggle Plain Text)
cmdSaveSettings = new JButton("Save Settings"); this.cmdSaveSettings.addActionListener(this); txtPlayerOneName = new TextField("Player One", 15); this.txtPlayerOneName.addMouseListener(this); this.txtPlayerOneName.addActionListener(this);
Last edited by WolfPack; Mar 25th, 2008 at 11:22 am. Reason: corrected code tags.
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
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
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)
if(txtPlayerOneName.getText().equals(emptyText))
Last edited by VernonDozier; Mar 24th, 2008 at 7:19 pm.
•
•
Join Date: Nov 2007
Posts: 28
Reputation:
Solved Threads: 0
Sorry I don't understand, message saying program is inside ActionPerformed?
From what I got from that was
but thats not even the correct syntax...Sorry I'm new to this, could you explain in a bit more detail?
From what I got from that was
Java Syntax (Toggle Plain Text)
JOptionPane.showMessageDialog(add.ActionListener.this)
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
Something like this:
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.
JAVA Syntax (Toggle Plain Text)
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); } } }
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
----------------------------------------------
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
![]() |
Similar Threads
- need help with jbutton and new jframe (Java)
- using the setText() method - getting error cannot find symbol (Java)
- I'm having problems (Java)
- Inventory program part 5 help (Java)
- Getting values from other class.. (Java)
- Search Method help needed!!! (Java)
- How to display a HTML file (in browser window) from Java Program...? (Java)
Other Threads in the Java Forum
- Previous Thread: 55: ')' expected (Error cant compile) PLZ HELP!
- Next Thread: java problem
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer j2me japplet java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree unlimited variablebinding webservices windows






