Skip to main content

The correct way to do things with swing.

No replies
Joined: 2010-02-04
Points: 0

I've gone through the swing tutorials examples for a basic application specifically this one:
and come into a number of problems. Biggest problem seems to be that nobody knows exactly how things should be done, the standard neat way to do things. When somebody built swing, I suspect that they had in mind a certain way, or set of ways in which people should use swing, otherwise they would not have bothered putting much thought into developing an action/event architecture.

So now I want to find out once and for all, given the scenario below, "how it's done".
by that I mean, What classes need to be created, what methods those classes should contain, what interfaces certain classes should implement, where classes are to be instantiated, who instantiates which class, and if any references to classes are held in private fields, this should be stated too.

What I'm going to pretend I have:
A Swing Form & Java file containing all the code to a simple GUI which has 2 buttons, each which perform different tasks, and 2 listboxes filled with numbers.
Each task is defined in an algorithm, call it AlgorithmA, AlgorithmB.
There is a main class (

Now the algorithms should be arbitrary, but for the sake of example, lets say AlgorithmA generates all even numbers up to 50. and AlgorithmB generates all odd numbers up to 50.
When I click button 1, algorithmA should be fired (somehow), and a model containing the result (arraylist or something) should be used to store the result. The listbox should immediately reflect the changes in the model.

Similarly for button 2 etc...

What I'm looking for is a proper MVC (not that crappy "[m + vc] that was slapped together as some kind of 'useful' tutorial" solution) solution. One that uses actions and actions listeners properly and places them where they're meant to be.

I've made the problem simple by only having 2 algorithms and 2 buttons and 1 main window. But I want to see the problem as if I had 50 algorithms, 3 windows, etc... as if it's a real complicated GUI.

From what I've seen, the tutorial doesn't follow any of the software design principles. Open-Closed, using strings to switch in potentially huge if-statements and tooltip strings to store information, it doesn't look like MVC to me, whatever it is.

This is not an opportunity to debate the importance of design principles, this is me trying to get to the bottom of the correct way to do things in swing on a large-ish project, because so far there is absolutely bugger all on this on the internet, and nobody in the IRC channel could come up with a definitive answer to this problem.

I made an attempt to solve this problem myself, I wanted to encapsulate the algorithm in it's own class, have a controller co-ordinate the execution of this algorithm by listening in for click actions from the view, but despite investing time into creating AbstractActions, Concrete actions, Controllers, Models (my own), and the views (in netbeans), and avoiding "oh just make it a singleton and be done with it" at all costs, I have come up with nothing but a cluster-f*** of useless classes, since I -still- cannot reasonbly wire my Button to my ActionListener. since neither of them are in eachothers scope. To do this, I may as well have stuck it all in one giant class and be done with it.

I genuinely look forward to hearing any reply.

Message was edited by: terabyteuk