Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

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

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!
Posts: 70382
  • 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
Posts: 70382
  • 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.
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic