How to write an interface that extends window listener

  • Context: Java 
  • Thread starter Thread starter BiGyElLoWhAt
  • Start date Start date
  • Tags Tags
    Interface Window
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 2K views
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.
 
Physics news on Phys.org
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   Reactions: 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.
 
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   Reactions: jedishrfu and jim mcnamara