How to write an interface that extends window listener

In summary, the programmer is looking for an interface that they can extend to catch all the methods that they don't want to override in their window listener. They are looking for a catcher for all the methods that they have to override. They are using the mvc paradigm where they create a view class that extends JFrame and throw property events that their main program captures.
  • #1
BiGyElLoWhAt
Gold Member
1,622
131
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
  • #2
Langauge and what youve looked at would help but we can't write for you.

Have you looked for examples online?
 
  • #3
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.
 
  • #4
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
  • #5
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.
 
  • #6
Java is not a language with a compact notation. It can be very pedantic at times.
 
  • Like
Likes BiGyElLoWhAt
  • #7
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

1. How do I create an interface that extends WindowListener?

To create an interface that extends WindowListener, you will need to create a new interface class and use the "extends" keyword followed by "WindowListener". This will allow the interface to inherit the methods defined in the WindowListener interface.

2. What are the methods that need to be implemented in an interface that extends WindowListener?

The methods that need to be implemented in an interface that extends WindowListener are windowOpened(), windowClosing(), windowClosed(), windowIconified(), windowDeiconified(), windowActivated(), and windowDeactivated().

3. How do I use the methods in an interface that extends WindowListener?

To use the methods in an interface that extends WindowListener, you will need to implement the interface in a class and then override the methods with your own code. These methods will be automatically called when the corresponding event occurs.

4. Can an interface that extends WindowListener be used for multiple windows?

Yes, an interface that extends WindowListener can be used for multiple windows. This is because the interface methods are automatically called when the corresponding event occurs on any window that is registered with the interface.

5. How can I register an interface that extends WindowListener with a window?

To register an interface that extends WindowListener with a window, you will need to use the addWindowListener() method on the window object and pass in an instance of the interface implementation class. This will register the interface with the window and allow the methods to be called when the corresponding events occur.

Similar threads

  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
27
Views
14K
  • Programming and Computer Science
Replies
3
Views
776
  • Programming and Computer Science
Replies
14
Views
4K
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
10
Views
2K
Back
Top