• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
ilaria venturini
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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!
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • 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()) ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!