• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to structure GUI code.

 
Eoin Brosnan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all.

I have a Jframe GUI that is used to insert, update and search a database. This is divided over two tabs- an insert/update, and search tab. Right now I have a class for each tab, which is something I can't really justify. However when I look at either class they both look extremely cluttered, and I can't see how combining them will help matters! I assume part of my problem is that I started using Jigloo GUI builder half way through!

So I guess what I am asking is, are there guidelines on how to layout GUI code, or is it normal for it to be very 'messy'?

cheers.
 
Carey Brown
Bartender
Posts: 2236
27
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GUI design is inherently messy. If you stop to think about it you are asking the computer to do lots of things which causes you to have to design and code all of that behavior. Swing gives you the building blocks you need but doesn't guide you in the clean assemblage of those blocks.

First of all, separate your data from the Swing widgets (i.e. use some semblance of the Model-View-Controller paradigm).

The other problem with GUI code is that the complexity increases exponentially as the project size increases. To keep the scaling under control consider using a Swing support library such as the one offered by JGoodies (free at www.jgoodies.com). To layout your screens quickly consider using a drag-n-drop GUI layout tool such as Jigloo for Eclipse (free for personal use at www.cloudgarden.com/jigloo/).

Another reference:
martinfowler.com/eaaDev/PresentationModel.html

The above references aren't necessarily for beginners but you may grow into them; you are already asking the questions that would allow you to appreciate some of the power of these tools.
 
Eoin Brosnan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
I am already using MVC- well that is the plan anyway! I had a brief look at jgoodies and I think the sample code alone should provide me with some of the answers I am looking for.

regards, eoin.
 
eric aro
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is my cookbook:

of course make a heavy use of MVC (and don't forget that there are THREE stuff in MVC)

design the view in a visual builder, and DO NOT use code generation. (like glide does, of jofrmdesigner or netbeans). Not generating code its important. These tools generates an XML UI file.

create a controller class, and use injection ( with annotation in my case) to get the necessary widgets from the XML UI file, and "mark" the callbacks (signals in glide, event in jfromdesigner).

As a rule of thumb, controllers should not store anything. There are two kind of controller ( for me) single and collection (that's my own private naming convention, do not expect to see it ini the outside world).

single controllers controls a single model and a single view.
collection controller handles a collection of models. Therefore the main algorithm for a dynamic controller is to find the right static controller for the given model object. ( using either class name, or any other information ).

When you're gui starts growing, you'll find necessary to share some graphical features among controllers. For instance, an error displayer (something that make a panel visible in the north, with the error message), a tab appender/manager (like in firefox) etc. .
Design it as controller service. (a service to display an error, a service to ask for a file etc.)
Creates interfaces for each of these services, and use injection to get those services.

Whatever the injection service you use ( spring, iOsgi, or your own, it's easy to do) this structure is quite robust :

widgets layouting in an XML UI
controllers that focus on there job
models
shared "services" among the gui, that is passed through the code using injection.


You can start writing useful components.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!