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