java - Should action listeners be in the class they apply to? -


i'm programming web browser multiple classes. 1 class "navbar" holds of buttons such search, back, forward, etc. navbar class has action listeners which, when search button pressed, become active, , lead production of url, needs passed jeditorpane, editor pane in different class, "editor".

it not make sense editor instantiated inside navbar, how can pass variable navbar class editor class?

is okay use statics in situation?

why not give nav bar reference editor? done in constructor, if editor instantiated first, or via getter/setter pair (actually, setter needed...). if nav bar has such reference, can call appropriate methods within listener implementation.

class editor { ... } class navbar implements actionlistener {     public navbar(editor editor)     {         myeditor = editor;     }     public void actionperformed (actionevent e)     {         // call methods of myeditor     }      private editor myeditor; }  editor theeditor = new editor(); navbar thenavbar = new navbar(theeditor); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -