Some clarifications about Adapter pattern applied to Swing events in Java -


i studying java swing , how handle event using adapter pattern not override of methods handle event.

i have found short example , want know if have understand it:

import java.awt.event.windowadapter; import java.awt.event.windowevent; import javax.swing.jframe;  public class sketcher {     jframe window = new jframe("sketcher");      public sketcher() {         window.setbounds(30, 30, 300, 300);         window.addwindowlistener(new windowhandler());         window.setvisible(true);     }      class windowhandler extends windowadapter {         public void windowclosing(windowevent e) {             system.out.println("closing");             window.dispose(); // release window resources             system.exit(0); // end application         }     }      public static void main(string[] args) {         new sketcher();     } } 

what have understand is:

the sketcher class contains main method creat new sketcher instance.

a sketcher instance create new jframe object show frame on monitor.

so when create new sketcher oject created new jframe object.

and here have first doubt (that genera java doubt):

why not creating jframe windos object inside constructor of sketcher class?

whatever, in constructor, set property jframe object , add windowlistener jframe.

now addwindowlistener new windowhandler object custom object handle windows events.

now have 2 possibilities handle these events:

  1. use classic listeners: in case have implement specific listener possibile events can occur on jframe

  2. use adapter (like in case), in case use internal class named windowhandler extends class windowadapter. class windowadapter contains void methods possibile jframe events. in windowhandler can define method want handle , not methods.

is reasoning right? tutorial example or present problem can't see?

tnx

andrea

your reasoning correct, here notes:

  1. you asked question why not creating jframe windows object inside constructor of sketcher class?

    the compiler doing work you; places initialization of jframe inside constructor. explicitly place jframe initialization in constructor.

  2. your windowhandler class doesn't have inner class; class implements windowlistener or extends windowadapter.

  3. the xxxadapter classes in awt , swing naming convention classes provide no-operation convenience implementations of related interface. aren't adapters (see below).

  4. your main implementation doesn't have in frame's class; in class.

generally, don't create bunch of things inside constructor, if there side-effects. it's better provide separate construction , initialization methods.

specifically swing, it's typical subclass components provide ui specialization needed application, including jframes. keep business logic separate.

even though swing class named windowadapter, isn't adapting in sense of adapter pattern. provide default no-operation implementation of methods of windowlistener interface, allows developer override method s/he's interested in.

so more study of overriding adaptation; latter used make 2 incompatible apis work together.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -