I am new to event based code. I am writing a Swing application from scratch. I have one main Swing class for displaying UI components and a Listner class implementing ActionListener interface.
I have made all components related to ParentGUIWindow as "static" because the ParentWindow will be displayed at the start of application. There wont be any multiple instances of different components. Hence thought of keeping them static.
Now, problem is as all of my GUI components are static , so i am forced to make the listener object static as well. I want to know, will it be fine if I make reference of listener ( object of the class implementing ActionListner) as static in SwingMain class ? Is there any harm to have a static listener object for entire application or should i go with "this" implementation done with non static components?
Please find code below:
I want to design and code in the most efficient manner. Please help me to do a good design.
You will need a different Listener for each button, surely.
So make the createButton method return the button, and pass the Listener as a parameter. Have a look through a book which shows you how to create Listeners as anonymous classes, eg Deitel or Horstmann and Cornell, etc. I haven't written about that for some time, but I still think I was correct in my rule of thumb here and here. See whether those posts help, and the older links therein.
You will end up with something like this . . . once you have corrected all my spelling errors.
I don't like having all your members static.
subject: Design help - Need help in "static" implementation with Swing application