How do you use event handler in a separate class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do you use event handler in a separate class?

I have a separate Handler class, that implements ActionListener. It overrides the actionPerformed method and everything is candies and butterflies. But wtf i use it in different class: button.addActionListener(new Handler()); And it screams „couldn’t resolve symbol „Handler”.. What am I supposed to do?

16th Jun 2022, 5:21 PM
Nick
8 Answers
0
Did you make sure to import it?
16th Jun 2022, 5:30 PM
Justice
Justice - avatar
0
This is swing or javafx? I assume it's Swing. b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ // do something } }); you should do something in this way.
16th Jun 2022, 5:31 PM
Satyam Mishra
Satyam Mishra - avatar
0
This is Swing. Now, while i fiddled around: I have a separate Controller class, in which i put the Handler class. In this case, first forces me to write Controller.Handler() And after that, it says „Controller is nit an enclosing class” ( dunno what that even means) When I take the Handler class outside the Controller, I can use the Handler() just fine. But this is incorrect solution, I need to make the Controller in charge of action listening..
16th Jun 2022, 5:40 PM
Nick
0
Ok I had to declare the Handler class as static… But guys thanks anyway, it just tend to be so frustrating.
16th Jun 2022, 5:42 PM
Nick
0
What does swing use for its gui? Just asking, I mean javafx uses fxml files.
16th Jun 2022, 5:44 PM
Satyam Mishra
Satyam Mishra - avatar
0
I only know it uses the javax.swing library for layouts and JComponents, unfortunately I’m still learning and don’t know the low level details
16th Jun 2022, 5:51 PM
Nick
0
Okayy
16th Jun 2022, 5:53 PM
Satyam Mishra
Satyam Mishra - avatar
0
I dont knos swing, but im currently working with stuff like this in python. If your event listener is in the class Handler, then you might be able to reference it by class (ie, Handler.your_event_listener) also the keywords self,root,parent, children etc can be helpful as well... just what ive learned (with python at least)
16th Jun 2022, 7:57 PM
madeline
madeline - avatar