I've been working on a simple game with a GUI and some buttons, and I'm having trouble using the ActionListener. I want to have my ActionListener instructions in my main class "FinalGame.java", and I want to send it to the UI class "UI.java". I tried to pass it as a parameter and it isn't working. What should I do?
#How do I implement an ActionListener from a different class?
1 messages · Page 1 of 1 (latest)
What is you actual end goal?
The simple answer is, when you action is called, use the ui attribute you have to call a method from it and do whatever you want.
So, instead of passing the choice handler to your ui, try the opposite.
Also, you might be interested in checking out MVC. This would help you make simpler interactions between your components
ActionListener can work as an external class or as a closure (inner class or anonymous inner class or private class)
in your case, ChoiceHandler is a non-static inner class
private ChoiceHandler choiceHandler;
public void createUI(final ChoiceHandler choiceHandler) {
this.choiceHandler = choiceHandler;
...
}
button.addActionListener(choiceHandler);
also all your elements in UI are "package protected"
in your specific case why not put ChoiceHandler in it's own public class ?
like what are you trying to achieve "glue code wise" ?
you can simply have this:
// final is important here
final JButton button = new JButton();
// final this proxying
final UI parent = this;
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent event) {
/* ... any final variable can be called from here... this anonymous class is also known as functional interface via @FunctionalInterface and a Java closure */
parent.frame.setTitle("CLICKED");
}
});
similar to JavaScript closure:
button.addActionListener(function(event) {
// ...
});
hey @torpid jetty thanks for the help
I've decided to put choicehandler in its own class, but i don't really know how to instantiate it and call it across the ui and main game classes
well, if you try to glue things, you want the closure approach
or at least extend the public class into a closure
otherwise pass everything in the ActionListener constructor
If you tell me what should happens when you "click" then I can tell you few approach 😛
@torpid jetty hey, so I've "finished" the game I was trying to work on.
now im thinking I just leave choicehandler alone and instead just toss the story methods into another class since they take up the bulk of the code
new Random().nextInt() is wrong you should have only one static instance and not reseeding, also use
private static final SecureRandom RANDOM = SecureRandom.getInstanceStrong();
("" + energy); should be (Integer.toString(energy))
if (treasurePicked.equals("Lead")) {
always use YODA style IF
if ("Lead".equals(treasurePicked)) {
or even better enum
String[] treasureOptions = {"Copper", "Zinc", "Nickel"};
should be extracted static final
with enum you could avoid
if (treasurePicked.equals("Iron")) {
money += 300;
mainTextArea.setText("You found Iron!"
+ "\n\n(Well, this just cured my iron deficiency.)");
}
else if (treasurePicked.equals("Platinum")) {
money += 2000;
mainTextArea.setText("You found Platinum!"
+ "\n\n(This should sell for a HEFTY amount.)");
}
else if (treasurePicked.equals("Gold")) {
money += 5000;
mainTextArea.setText("You found Gold!\n\n"
+ "(GOLD. GOLD. GOLD. YES.)");
}
with just
if (treasurePicked.isValid()) {
money += treasurePicked.getMoney();
final String text = treasurePicked.getText();
mainTextArea.setText(text);
}
you could also refactor GameEvent into another enum
but your game works fine
Hi, thanks so much for your help
I'm definitely going to try to clean up my code with your suggestions
But, I still can't figure out how to separate the code in my game to different classes
I tried putting some of the methods into this class and then calling them in the main code using story.(method name), and it causes a bunch of errors right after the GUI opens
I hope I'm not bothering you too much
try Eclipse J2EE IDE, refactor functionality 😉 extract code into method / class
your game core logic should not use any AWT
basically, you should be able to write a console text game with the same core as a textual view
or as a web game with an HTML view, also makes your code easier to unit test with a "predefined seed" to avoid random
I played around with the refactor function and I eventually got it to do what I needed, thank you so much!