A wise design for a swing application ?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Friday, October 3, 2003 12:07 AM

Hello. I'm a cute little Java-developer newbie! :D I'm making an application that's going to have a swing GUI, and some networking functionality. Well.. my problem is that i don't know how to avoid getting tangled in a foolish design with the application. I've got a GUI with many different sections that have different buttons and so on, and because of that, i need a button listener ( ActionListener ) reference that's accessible whereever i add buttons to the JPanel s. Now then, this button listener needs to have references to some other objects that it needs to "command" or use when a certain button is pressed.    More>>

Hello.


I'm a cute little Java-developer newbie! :D


I'm making an application that's going to have a swing GUI, and some networking functionality.


Well.. my problem is that i don't know how to avoid getting
tangled in a foolish design with the application.


I've got a GUI with many different sections that have different buttons and so on, and because of that, i need a button listener ( ActionListener ) reference that's accessible whereever i add buttons to the JPanel s.


Now then, this button listener needs to have references to some other objects that it needs to "command" or use when a certain button is pressed.


So far it has seemed like i need to pass references of some objects "down" to any parts of the GUI that have buttons.. And i get the feeling that there's got to be a better way of doing things :)


a little explanation :


a "Main class" that contains a JFrame that contains JPanel #1 that contains JPanel #2 that contains buttons..


Now the main class contains references to some other objects that need to be able to for example, append a text field that's on the GUI, and the buttons in JPanel #2 need to be able to call some methods on the objects that are in the main class and so on !


Sounds messy ? :) - It is.



I've been extending JPanel to make parts of the GUI, and passing a reference to the button listener to each part in its constructor.. The problem is that the button listener ( ActionListener ) needs to access many other objects, so i can't instantiate a new listener class in every part of the GUI and making it an inner class wouldn't help either.. as far as i know :) (because it needs the references to other objects)


Maybe someone could suggest a more clever approach to making a swing application ? - Any help would be really appreciated.


Making some central things static might not be possible because i want to be able to use multiple instances of the application for testing.


And some solutions might be just too elegant for me to comprehend, so if you'd like to help, please try to keep your suggestion relatively simple :)


Thanks in advance! :)

   <<Less

Re: A wise design for a swing application ?

Posted By:   s_kandacharam  
Posted On:   Saturday, October 11, 2003 07:16 PM

There is a better way to handle this kind of problem. You can use Observer or Mediator pattern to solve this problem. I suggest you to read Gang Of Four(GOF) design patterns. These design patterns suggests solution for known issues like this. And use of GOF patterns is a good practice. Swing is the only API where we can use all GOF patterns. So, it's worth to read it.

Hope this helps for your design issue.

Kandacharam

Re: A wise design for a swing application ?

Posted By:   abu_abdul  
Posted On:   Friday, October 3, 2003 12:58 AM

Well.
Dood am a new developer as well, dont even know if i should call my self that.
But in any case. Am not sure this is more eligant but, have you considered using package scope variables and protected variables for all variables you want to be visible throughtout. If you have any variable clash between classes then something called variable hiding will occure. In such a case use the full stretch name eg. Class#1.pannel#2.variable3 = ....
I am sure you know that package scope variable means that the variable is visible in all classes in the same package.
In other words declare all variables and objects you want to be used anywhere package scope as follows:

type variableName;
class className; (even inner classes; you can retrieve them as: Innerclass v = new Outerclass.Innerclass())

Hope this helps and its not too much beneath you
About | Sitemap | Contact