Calling method cant get the parameters right
im trying to call a method in my main class (GUI) from a different class (ActionHandeler)
heres the method in GUI:
Code :
public static void main(String[] args) {
// Schedule a job for the event dispatch thread:
// creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUI gui = new GUI();
// Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
gui.launchFrame();
action.actionPerformed(null);
}
});
}
here is 'actionPerformed' in ActionHandeler:
Code :
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd != null) {
if (cmd.equals("submit")) {
try {
int guessed;
String input = g.type.getText();
int value = random(10);
guessed = Integer.parseInt(input);
if (guessed < 1 || guessed > 10) {
JOptionPane.showMessageDialog(null, guessed
+ " is not a valid number!");
} else if (guessed == value) {
JOptionPane.showMessageDialog(null,
"You guessed it right! :D");
wins += 1;
} else {
JOptionPane.showMessageDialog(null,
"Sorry the correct awenser was " + value
+ ". :/");
losses += 1;
}
} catch (Exception e1) {
}
}
}
}
I know that the parameters cant equal 'null'
Code :
action.actionPerformed(null);
I tried making it 'e' and 'ActionEvent' but its not working
any help would be appreciated!
Re: Calling method cant get the parameters right
This seems like a bad design. Shouldn't you be adding the action to something else that calls actionPerformed?
But if you really want to go this way, there's nothing stopping you from constructing your own instance of ActionEvent.
Re: Calling method cant get the parameters right
Why are u calling the event's method? I'm not sure how efficient that may be. But if you can't find a solution, i suggest putting all that code in actionPerformed and creating your own method, and when an event occurs you just call that method from actionPerformed. Same with your main class, call your method from the main class instead of the actionPerformed. This way the method of your code is not limited to listening to ActionEvent variables only.
Re: Calling method cant get the parameters right
i fixed it. I implement ActionEvent into my main class instead of going to a different class to handle it