Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java How to write an interface that extends window listener

  1. Mar 21, 2017 #1

    BiGyElLoWhAt

    User Avatar
    Gold Member

    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.
     
  2. jcsd
  3. Mar 21, 2017 #2

    jedishrfu

    Staff: Mentor

    Language and what youve looked at would help but we cant write for you.

    Have you looked for examples online?
     
  4. Mar 21, 2017 #3

    BiGyElLoWhAt

    User Avatar
    Gold Member

    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.
     
  5. Mar 21, 2017 #4

    jedishrfu

    Staff: Mentor

    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
     
  6. Mar 21, 2017 #5

    BiGyElLoWhAt

    User Avatar
    Gold Member

    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.
     
  7. Mar 22, 2017 #6

    jedishrfu

    Staff: Mentor

    Java is not a language with a compact notation. It can be very pedantic at times.
     
  8. Mar 28, 2017 #7

    BiGyElLoWhAt

    User Avatar
    Gold Member

    In case anyone is interested, this was my solution. Apparently all of the closing/resizing/etc stuff happens in 'Event.WindowEvent'.
    Code (Java):
    import java.awt.event.WindowEvent;

    import java.util.EventListener;

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

    }
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: How to write an interface that extends window listener
  1. How to write a driver (Replies: 1)

Loading...