I've mostly programmed backend servers, and enjoy Java a great deal.
I've gotten to a point where I have to write some GUI for my backend
machines. I dislike most gui code I've seen (not that much) because
it appears to me to be a complete mess. I used a gui designer and it
produced a huge java file that I would be embarrassed to put my name
on.
Is their a good method to writing nicely coded gui's. I'm not so
concerned with what the ui should look like (there is a lot of good
info about that), mostly about how to design and code a gui in a good
manner.
I've read a lot on MVC, but I rarely see code that reflects what I
think good MVC should be. Is this a common problem, am I going about
this all wrong?
Jason
Roedy Green - 03 Jan 2008 22:18 GMT
> I dislike most gui code I've seen (not that much) because
>it appears to me to be a complete mess
When I first saw GUI code, for a prototype MacIntosh on the Lisa, I
said, "You have got to be kidding. Nobody is going to go to all this
fuss for a simple app. I was wrong."
It has not improved that much.
Some things you can do.
1. Subclass components to put in all your standard tweakings. That way
you can change your "style" in one place and reduce the clutter. We
need something the equivalent of CSS style sheets for Java GUIs to
hide the details of layout and look. That goes much further than just
subclassing.
2. use nested panels.
3. separate the allocation and field attributes from the layout. Keep
business logic separate from both.
4. make great use of the "extract constant" feature to parameterise
your code with named constants. It is a lot easier to maintain a
named constant that dig in the code for multiple places to change
something.
5. keep your attribute setting methods is a standard order.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Karsten Lentzsch - 04 Jan 2008 08:57 GMT
> [...]
> Is their a good method to writing nicely coded gui's. I'm not so
[quoted text clipped - 5 lines]
> think good MVC should be. Is this a common problem, am I going about
> this all wrong?
I recommend other patterns than MVC. For me MVC is good
as a component pattern, for applications there are better
patterns.
1) As with MVC, separate the domain from the rest.
2) Almost always separate the GUI construction, configuration,
layout, and fine grained events (e.g. mouse over) from the
presentation logic; hence, use an MVP variant or "Presentation Model".
The choice between MVP and "Presentation Model" depends on
the toolkit, associated binding support, etc. For Swing,
I favor to follow the "Presentation Model" pattern, because
several Swing core components already use this pattern.
I provide a free presentation regarding Java desktop patterns
that presents and compares MVC, MVP, and Presentation Model,
see http://www.jgoodies.com/articles/ I also provide a free
binding library that assists in using the Presentation Model
pattern, see http://www.jgoodies.com/downloads/libraries.html
Just as a side note. I recommend to favor flat over nested panels
for editor that are perceived as a single sheet. Nesting in Swing
means loosing layout constraints, grid information, and hence
ends up often in unaligned, arbitrary component positions.
-Karsten