| | |
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,836
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,836
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
actionlistener android api apple applet application apps arguments array arrays automation balls binary bluetooth card chat class classes client code component consumer database draw eclipse ee error event exception file fractal free game gameprogramming gis givemetehcodez graphics gui helpwithhomework html ide image input integer j2me j2seprojects java javaprojects jmf jni jpanel julia jvm linux list loop machine map method methods migrate mobile myaggfun netbeans newbie nextline nls notdisplaying number oracle output print problem program programming project recursion recursive scanner screen security server set size sms socket sort spamblocker sql sqlite string sun swing terminal test threads time tree trolltech windows






