It's not a secret anymore!*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Author

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,
Kacee
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
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: http://aspose.com/file-tools
 
subject: Design help - Need help in "static" implementation with Swing application
 
Similar Threads
What is the advantage of adding custom listener to panel
Facing Exception in a simple swing program
How to handle events in java
How do I pass a frame into another class?
Null pointer exception in button event handler.