aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Swing desktop application best practices Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing desktop application best practices" Watch "Swing desktop application best practices" New topic
Author

Swing desktop application best practices

David Stevens
Greenhorn

Joined: Aug 25, 2008
Posts: 19
Hi, I'll attempt to ask a less idiotic question this time

Can anyone recommend a good book (or web-site tutorial) that deals with the architecture and design of medium/large desktop applications. I'm not talking about the use of individual components, but more about how such an application might be put together. I have read about and looked at code which uses 'Model View Controller' but I'm not sure how to apply this to a large scale application.

I suppose I'm looking for a book that covers a higher abstraction level, then one which just covers how individual components work.

Any help on this is much appreciated.

David
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Are you talking about Design Patterns?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
David Stevens
Greenhorn

Joined: Aug 25, 2008
Posts: 19
From what I understand, design patterns are at more of a middle tier of abstraction. I'm really trying to get a handle on more of an overview of how, in particular, desktop applications are modeled.

I appreciate that this will depend on the actual application to some extent, but aren't there 'best practices' or idioms, that professionals have adopted. And if so, are there any books or in-depth articles that discuss these.

I'm sorry that my question might be a little vague, but I am eager to learn as much as I can. I posted the question here because I am interested in how desktop applictions are put together in practice.

Regards,

David
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

David, the information you are looking for is kind of like the Holy Grail. Everyone is looking for it. No one can find it. There are some new frameworks/libraries emerging lately to try and help define what best practices are for developing Swing applications but they are so new they are likely to change before any major releases and then its still what that group of developers thinks is best. There is hardly an industry standard at this point. Sad but true.


GenRocket - Experts at Building Test Data
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi David,

I think the biggest problem with Swing is that it's a really, really huge library or framework. If you start with Swing there are simply so many classes and concepts that it's almost impossible to get everything right the first time you use it.

I recently developed a rich client application based on the NetBeans platform which is well-suited even for medium and large applications and in fact it helps a lot with the general application design because it provides a scaffold for your applications. BUT it's not as easy as it may seem at first glance because you can't effectively use such a framework completely without understanding what's going on behind the scenes.

For this I would say the only thing that really helps you if you're relatively new to Swing is your own experience!!! Just try and experiment a lot with what you think is a good application design. Don't expect to get an ultimate solution for every situation and GUI application. Of course you should take care of a good overall design in your application but don't focus too much on Swing specific things. For some advices it could be helpful to additionally study a framework like NetBeans to see some concepts and best practices they use.

Marco
David Stevens
Greenhorn

Joined: Aug 25, 2008
Posts: 19
Thanks Gregg & Marco. I appreciate your replies.

I guess that there's often no substitution for experience and seeing what works and what doesn't. I suppose it's a case of reading lots of good code & trying to emulate it in my own work. I realise that there's no 'one size fits all' solution. It's just that there are lots of great books around that teach the 'buidling blocks' so I was hoping that there might be more info on how it all fits together.

I will look further into building on top of the NetBeans platform. I know there is some info about on the website.

Would anyone happen to know about any open-source desktop applications so that I could study the source code?

Many thanks,

David
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

David, I forgot to mention the Swing Application Framework. This is an official attempt to simplify and solve common tasks for GUI applications (based on Swing). Perhaps the homepage provides you additional information on what you're looking for or you can even use the framework to create your own applications.

If you really want to try rich client programming on top of the NetBeans platform you'll find lots of information on the NetBeans homepage, mailing lists, tutorials etc. But be warned to take enough time to learn! As the platform is a very huge framework you shouldn't expect a good understanding and outcome too soon. On the other hand the learning effort pays out later and you can make faster progress in development.

Marco
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Originally posted by Marco Ehrentreich:
David, I forgot to mention the Swing Application Framework. This is an official attempt to simplify and solve common tasks for GUI applications (based on Swing). Perhaps the homepage provides you additional information on what you're looking for or you can even use the framework to create your own applications.


This is one of the young frameworks I was talking about. The project just got a new leader and from what I understand the code base is a total rework at this point. I'd just be cautious until some of these things get some time under their belt and the API gets more stable. Nothing worse while learning something new than API changes. It can be really frustrating.
David Stevens
Greenhorn

Joined: Aug 25, 2008
Posts: 19
Thanks again guys.

I'm actually using NetBeans 'Java Desktop Application' atm, which as you probably know is based on the Swing Application Framework (JSR296) for my current test project (a hotel management system). While this is great for getting me started and it saves me hand-coding the basic Jframe etc... it doesn't really offer me any guidance as to how to go from there.

I started by coding a lot of the application's classes (up to about 30 right now) and I'm now starting on the GUI. I have a 'Hotel' singleton which in it's parameterless constructor, creates about 10 'controller' classes e.g. (roomController = new RoomController(this); ). My app then starts the GUI again with a reference to the hotel.

So basically, there's a Hotel reference in my main GUI (and the windows that open from it) and all the controlling classes that deal with aspects of the application. I'm worried that I'm sharing too much with all my app. How does this sound to you?

btw. saw your interesting blog on this kind of subject Gregg.

Many Thanks,

David
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

it doesn't really offer me any guidance as to how to go from there.

I think this is the same problem we already talked about. It's simply not possible or to specific to guide someone to a solution for his/her special application. Don't get me wrong, I had been searching perfect solutions too. So I really know what's your problem. But it's better to accept that no matter what frameworks or libraries you are using there's still some work left you have to do on your own

I started by coding a lot of the application's classes (up to about 30 right now) and I'm now starting on the GUI. I have a 'Hotel' singleton which in it's parameterless constructor, creates about 10 'controller' classes e.g. (roomController = new RoomController(this); ). My app then starts the GUI again with a reference to the hotel.

Probably even these few sentences would be enough for some design gurus to discuss for days :-) Should you use constructors or better factory methods? Is it good design to have singleton objects or not? It's nearly impossible to tell if this is good or bad without having a deep understanding of YOUR application. If your application or at least the business logic of it does what it's intended to do then you it can't be that bad. If it works there's still enough time to take your experiences and think about how to refactor and improve the design or code.

So basically, there's a Hotel reference in my main GUI (and the windows that open from it) and all the controlling classes that deal with aspects of the application. I'm worried that I'm sharing too much with all my app. How does this sound to you?

Obviously GUI components should be used to graphically present application data to the user or receive input events from the user. From that perspective GUI components clearly shouldn't know too much about other application details. On the other hand view components have to access some data to be able to present them to the user. Now it's up to you to decide if the GUI in your application knows too much

Sorry for not giving you more concrete answers or advices. I hope you understand that only you can give most of the answers to your questions

Marco
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

I've often found that trying to pass objects around from class to class just so I have access to something can become a bit of a maintenance nightmare. What I tend to do is create an Application class that works kind of like a Session (if you are familiar with web development). This Application is a singleton class and is accessed via:

Applcation.get();

The Application object has a reference to anything I need global access to. The main application frame for example might be a good candidate. Whether you allow direct access to it or not depends on your needs. In an application I am working on right now I have a JTree that needs to be accessed from forms and actions. So I have access to it from my Appliction.

Appliction.get().getTree();

Again, its up to you whether you want to give direct access or simply have utitlity methods that access it for you:

Application.get().updateTree();

Just an idea I have used. Obviously this isn't an industry standard and unfortunately Marco's response, while true, is way too typical. I'd really like to change that mentality. That's why I wrote that article on my weblog.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Gregg,

now I've read this article in your weblog, too, and I absolutely agree with all you wrote

I would really like to help anybody here with my best practices in Swing development but unfortunately I'm not (yet) what I'd call an expert in Swing. As long as I find myself almost endlessly improving my own code during development this is probably not a stable foundation for others to learn from. But as you wrote there are surely other developers with a lot more experience who could help in this situation.
And please don't get me wrong, I didn't want to give Dave the impression that it's useless to search for best practices and common solutions in this area of development! It's just because I experienced the same as you and still couldn't find really good answers to all of my question...

Marco
David Stevens
Greenhorn

Joined: Aug 25, 2008
Posts: 19
Thanks again guys. I really appreciate your input.

Gregg, your Application singleton sounds like it does the same job as my Hotel class, except you are pulling and I'm pushing . My Hotel class really only only does 2 things (so it's already doing too much according to Liskov's SRP - single responsibility principle ), it starts the application and it holds references to other parts of the program. I can see how 'pulling' might be preferable to 'pushing' since as you said, it doesn't involve passing the same object around in a lot of constructors.

On the subject of frameworks, I realise that it would be virtually impossible for any code framework to be able to incorporate the sort of structure I'm looking for. However, I had hoped that there might be information out there somewhere, as to how to model a 'general' desktop application.

Best regards,

David
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Originally posted by David Stevens:

On the subject of frameworks, I realise that it would be virtually impossible for any code framework to be able to incorporate the sort of structure I'm looking for. However, I had hoped that there might be information out there somewhere, as to how to model a 'general' desktop application.

Best regards,

David


You might want to look at Griffon. It does a good job of separation of concerns. It's Groovy based, FWIW.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi David,

it seems you are much concerned about how to get to your application data from any point within the application without having too much dependencies. Maybe you should have a look at:

1) The "Lookup" mechanism in the NetBeans platform.

2) The service discovery mechanism in Java.

3) IoC containers like PicoContainer.

I think you'll easily find the web pages with Google These mechanisms allow to discover implementations for interfaces, arbitrary data objects etc. at runtime while providing good design and loose coupling. Perhaps you can directly use these ones or you can get any ideas how to implement similar mechanisms on your own.

Marco
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Another good idea might be to start asking more specific questions in new threads. Break down exactly what you are trying to do and ask a question specifically about that. That might be a good way to get other people involved in your discussion. This thread has gone in a few related directions but it's a bit of a melting pot right now.
David Stevens
Greenhorn

Joined: Aug 25, 2008
Posts: 19
I can't thank you both enough.
You've been a great help.

I'll look into the things you've mentioned and post more specific questions as and when.

Cheers guys,

David
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing desktop application best practices