Java How to write an interface that extends window listener

  • Thread starter Thread starter BiGyElLoWhAt
  • Start date Start date
  • Tags Tags
    Interface Window
AI Thread Summary
The discussion revolves around the challenge of implementing the WindowListener interface in Java, particularly when only the windowClosing method is needed. The user seeks to create a new interface, tentatively named "windowSomethingCleverNameWise," that extends WindowListener but only requires the implementation of windowClosing, thereby eliminating the clutter of unused methods. The user expresses frustration with the seven methods that must be overridden and desires a cleaner code structure, especially since they are already extending JFrame.Despite exploring various solutions, including extending an adapter and creating anonymous classes, the user prefers a more streamlined approach. They consider the possibility of copying the WindowListener interface and modifying it to suit their needs but hope for a simpler solution akin to how MouseAdapter simplifies MouseListener. Ultimately, they propose a custom interface, WindowCloser, which focuses solely on the windowClosed event, indicating a desire for a more efficient way to handle window events without unnecessary complexity.
BiGyElLoWhAt
Gold Member
Messages
1,637
Reaction score
138
I am writing a program, and only care about one abstract method in window listener: windowClosing(windowEvent e){}

Because of this, I would like to write a new interface windowSomthingCleverNameWise that extends window listener and does nothing with all of the methods except for windowClosing, which would be abstract. This would clean up my code a lot. I am likely to use multiple interfaces in this project, and would like to keep the actual code as neat as possible. Plus, this way I'll always have it.

Basically, what I'm looking for is a catcher for all the useless methods that I have to override. Extending is not an option, as I already extend JFrame in my class. I need something I can implement.

*Edit
WindowListener has 7 methods that need to be implemented. Just annoying to look at while coding, and I'm sure my prof would get annoyed looking at all this crap as well.
 
Technology news on Phys.org
Langauge and what youve looked at would help but we can't write for you.

Have you looked for examples online?
 
Yea.
https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html
http://stackoverflow.com/questions/14185039/avoid-overriding-all-abstract-methods-in-java
and another one that I seem to have closed. It basically said interfaces can extend interfaces. They used the example of wanting to ADD methods, whereas I want to catch methods and not have to have them in my class that handles the actual listening. The stack overflow recommends extending an adapter. I can't do this without making separate classes for my window handling and my action handling. I don't really want to do that, but I guess if I absolutely have to in order to make this happen, I will. I am currently extending JFrame.

The language is Java. I thought that was the purpose of the beginning tag when you make the thread XD.
 
Sorry I didn't notice the tag. I thought it was java but didn't see it in your post.

I tend to use the mvc paradigm where I create a view class that extends JFrame and throw property events that my main program captures.

In your case, can't you just create an anonymous class to handle window events for the window closing and then close your program.

her's one programmer's solution:

http://alvinalexander.com/blog/post...swing-application-when-user-presses-close-but
 
  • Like
Likes BiGyElLoWhAt
I can, it just seems wasteful. I'm literally going to open a dialogue box on close. I was hoping for something I little more tidy, that I could add to a folder somewhere and just use that particular interface when i wanted to handle close only events, as opposed to the whole shabang. If there's not a way to do this easily, I suppose I can literally copy WindowListener to a new file, and delete the stuff I don't want in it, rename it something else and then I have effectively what I need. I just figured there might be someway to do what mouseAdapter does with mouseListener, but with an interface instead of a class, that would seem to be the simpler way.
 
Java is not a language with a compact notation. It can be very pedantic at times.
 
  • Like
Likes BiGyElLoWhAt
In case anyone is interested, this was my solution. Apparently all of the closing/resizing/etc stuff happens in 'Event.WindowEvent'.
Java:
import java.awt.event.WindowEvent;

import java.util.EventListener;

public interface WindowCloser extends EventListener {
   
    public void windowClosed(WindowEvent e);
   
       

}
 
  • Like
Likes jedishrfu and jim mcnamara
Back
Top