This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can someone explain to me how these custom-action listeners work?  RSS feed

 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I'm following a set of tutorials and I'm having trouble getting my head round these 'custom' action listeners.

So (to simplify it) say I have 2 classes and an interface.
Class 1: MainFrame which extends JFrame and connects all gui components
Class 2: A panel (with some random elements) which extends JPanel. MainFrame instantiates this class.
Interface 1: A listener for the panel

Class 1 (MainFrame)


Class 2 (FormPanel)


Interface (FormListener)



So I understand that the mainFrame creates an instance of FormPanel where the formPanel adds a number of elements, then the mainFrame adds the panel to the main JFrame. I also understand that the FormPanel has a listener which grabs the information FROM the filled in form, once the user presses the okButton. I get this, BUT:

1. how does the information flow (assuming it's from Panel TO mainFrame?)
2. how does the okButtonlistener get the information to MainPanel?
3. Why the interface?

Thanks guys!
 
Campbell Ritchie
Marshal
Posts: 55677
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume that code compiles.
Copy all the .java files into their own directory and navigate thither with the terminal/command line. Use ls(*nix) or dir(Windows®) to view the contents of that directory. Compile everything and see what you get. There will be a .class file called something like FormPanel$1.class. You can see that the anonymous class has created its own .class file.
Now, you have an object of the ActionListener attached to the button. What happens when you click on the button is that an Event occurs, creating an event object. That object is passed to the glass pane of the frame, which can do nothing with it, so it passes it to whichever Component is underneath that location, where the button has a listener which can intercept it. When the listener object intercepts the event, it fires the actionPerformed method.

You know that in Java8 you can replace the listener object with a functional feature (=a λ), something like
myButton.addActionListener((evt) -> {myAction();});
… where myAction is some method or other accessible from where you happen to be. Delete all the .class files and compile, and you will see the $1.class file does not reappear.

As for the form listener: don't know. If somebody has supplied you with that interface, there should be a description of it; if not, ask them. I am not convinced the Java Tutorials are much help on this point. There is an interface called EventListener and I presume that is what you are extending. I presume that means you can add that Listener and the form event will cause it to run its form event method.
 
Campbell Ritchie
Marshal
Posts: 55677
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I think you ought to initialise that form listener in the constructor. Otherwise it will be null and you may suffer Exceptions.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!