File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Design help - Need help in Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Design help - Need help in "static" implementation with Swing application" Watch "Design help - Need help in "static" implementation with Swing application" New topic

Design help - Need help in "static" implementation with Swing application

Kacee Saxena
Ranch Hand

Joined: Mar 04, 2008
Posts: 54
hello everyone,

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.

Thanks in advance,
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46328
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.
I agree. Here's the link:
subject: Design help - Need help in "static" implementation with Swing application
It's not a secret anymore!