• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic