java - Create an object instance of a subclass from an external class -


i've created application attaches events class of jbuttons , other swing components instantiate many times in gui class.

the class menuitemeventhandler attached each menu item. works fine when menuitemeventhandler class external. need inside gui class instead of external.

i'm left issue of not being able reference event handler subclass external class in same package. possible so?

below guiclass , eventhandler subclass

public class guiclass {  // gui behaviour      public class menuitemeventhandler extends abstractaction {        private string avariable;        private static final long serialversionuid = 1l;        @override       public void actionperformed(actionevent arg0) {           // update jlist added item       } } 

below external class want reference event handler can attach collection of gui objects.

it's making reference menuitemeventhandler class cant achieve.

public class menuitem {  resturantgui.menuitemeventhandler action = resturantgui.new menuitemeventhandler(this.item); newbutton.setaction(action);  // attach event menu item 

two options, either can make menuitemeventhandler static:

so declare class this:

public static class menuitemeventhandler ... 

or create event handler reference instance of enclosing class guiclass

something:

guiclass guiclass = new guiclass(); menuitemeventhandler handler = guiclass.new menuitemeventhandler(); 

personally, find second option smell of bad or incorrect design. never use kind of construction. example of how can "work around" kind of pattern (there others, depends of context):

public class guiclass {      public class menutitemeventhandler {            ...      }       public menuitemeventhandler createeventhandler() {            return new menuitemeventhandler();      } }  ...   guiclass guiclass = new guiclass();  menuitemeventhandler handler = guiclass.createeventhandler(); 

Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

c# - SharpSsh Command Execution -

python - Specify path of savefig with pylab or matplotlib -