• 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

Problems in implementing the listener of a GUI in a different class

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I have two different classes called Login (in the package View) and RegButtonListener (in the package Controller). I want to use the MVC pattern.

Login creates a GUI for login. I used NetBeans GUI Builder to create it.

In the class RegButtonListener there is the code for the click of the buttons, then various listeners of the GUI.

Now I want to close the login frame at the click of a button so I want to use the dispose() method.

If I had all the code (GUI code and code of the listener) in the same file, close the frame would be easy enough to do because this.dispose().
But in my case I have two different classes.

I also have a second problem.
In Login class (in the packege View) I have included the line regButton.addActionListener(Controller.RegButtonListener). However, Netbeans tells me an error "package regButton does not exist. <identifier> expected". Why does he consider regButton a package? How can I fix these two problems?

How do I do then?
thanks

 
Below is the code of the two classes.

Login class.


RegButtonListener class


Thanks!
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ilaria venturini wrote: Why does he consider regButton a package? How can I fix these two problems?


Short answer: Stop using the GUI builder which generates the code for you. Then all you need to do is have a simple code like


Long answer:
Besides this, you can try passing a reference of Login to your action listener class via the constructor and then invoke a dispose in your action performed.
Else obtain a reference to your regButton via actionEvent.getSource(). The try to find the top level window it belongs to (which should be the Login in your case) and then invoke a dispose on it. Check out SwingUtilities#getWindowAncestor
Can you do this via the netbeans UI builder? No idea. I prefer to handcode myself and have never used any DnD style UI builders
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do it much more concisely in Java8. I can't remember the exact syntax but it looks something like
myButton.addActionListener((evt) -> Login.this.dispose());
I am not sure whether it is in the Java Tutorials. No, it doesn't seem to be.
You can read about Actions in the Java Tutorials.
 
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

A few minutes ago, I wrote: . . . I can't remember the exact syntax

try
myButton.addActionListener((ActionEvent evt) -> Login.this.dispose());
instead if that didn't work.
 
ilaria venturini
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the tips. The problem of dispose() I solved thanks to getSource().
But I do not know how to solve the problem on the line regButton.addActionListener(Controller.RegButtonListener)..
 
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
So what is Controller.RegButtonListener?
If it is an inner class, that won't work. If it is a public field, that is poor design and the field name shoudn't start with a capital letter.
 
ilaria venturini
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Controller is a package. RegButtonListener is a class. In this class there is the code that is executeted when user click the button..
 
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
Packages should have their names in lower case throughout.
You cannot pass a class to a method as an argument. You must have meant something else. Do you mean (new controller.RegButtonListener()) ?
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic