How to write an interface that extends window listener

  • Context: Java 
  • Thread starter Thread starter BiGyElLoWhAt
  • Start date Start date
  • Tags Tags
    Interface Window
Click For Summary

Discussion Overview

The discussion revolves around creating a custom interface in Java that extends the WindowListener interface, focusing specifically on handling the windowClosing event while avoiding the need to implement all other methods of the WindowListener interface. Participants explore various approaches to achieve a cleaner code structure in their Java applications.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant expresses the desire to create a new interface that extends WindowListener but only includes the windowClosing method as abstract, aiming to simplify their code.
  • Another participant suggests looking for examples online and emphasizes that they cannot write the code for the original poster.
  • A participant mentions that interfaces can extend other interfaces, but they are looking to avoid implementing methods rather than adding new ones, and they consider using an adapter pattern.
  • One participant proposes using an anonymous class to handle window events, suggesting it as a potential solution for managing window closing events.
  • The original poster expresses concern about the perceived wastefulness of using an anonymous class and considers copying the WindowListener interface to create a simplified version.
  • A later post introduces a custom interface called WindowCloser, which includes a method for windowClosed, indicating a potential solution to the original problem.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to handle the issue of implementing the WindowListener interface. Multiple competing views and suggestions are presented, with no clear resolution.

Contextual Notes

Participants discuss the limitations of Java's verbose syntax and the challenges of managing multiple interfaces while extending JFrame. There is mention of the MVC paradigm and the use of event listeners, but no definitive solutions are agreed upon.

Who May Find This Useful

Java developers looking for ways to manage event handling in GUI applications, particularly those interested in interface design and code organization.

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
language 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   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.
 
Java is not a language with a compact notation. It can be very pedantic at times.
 
  • Like
Likes   Reactions: 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   Reactions: jedishrfu and jim mcnamara

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 14 ·
Replies
14
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
19
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 27 ·
Replies
27
Views
17K
Replies
3
Views
4K