views:

349

answers:

4

What I do, in NetBeans for example:

  • create a UI component class, suffixed with Swing component type, e.g. JPanel, e.g. MyUIWidgetJPanel
  • create a logic/behaviour class to handle the behaviour/logic, i.e. what happens when the UI component is used, e.g. buttons within it pressed etc., MyUIWidgetLogic

I may also link components/logic together using references, if the behaviour/outcome of one component influences/impacts another, e.g. some options displayed are no longer relevant, or to give context sensitive options.

What do you think of this? Good? Bad?

What do you do?

+2  A: 

I tend to use the Presentation Model Pattern.

Essentially it seems to be what you do: create a class which encompasses the logic, separate to the UI class. The UI classes shouldn't contain logic - only the code needed to display the interface.

You can then bind your front-end values to the back-end presentation model class using something like JGoodies Binding (I understand that Spring RCP is pretty good for this as well).

Phill Sacre
+1 and thanks because it mentions a real approach to implementation with examples. However the presentation of the text is a bit dry, a work in progress and uses UML - where I would need to remind myself what notations mean.
Rob
Accepted answer as three approaches were mentioned. Thanks to everyone else too.
Rob
+1  A: 

What you are describing is a design pattern called MVC (Model View Controller) It is not undisputed, but it is the most popular way of separating concerns in a GUI. It also ensures that when you have several representations (views) of the same data (model) you can be sure all of them are updated when the data changes. The Controller part of MVC takes care of telling the userinterface which parts should be enabled (amongst other things).

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

Thirler
Yes I'm familiar with MVC and similar things like separation-of-concerns. I'm looking for a concrete approach to implementation, because apart from the overall theory describing such concepts, there doesn't seem to be some *implementation* approaches - this is left to the developer.
Rob
A: 

Since Swing components communicate using Events via the Event Dispatch Thread, you typically will provide ActionListener implementations to the elements that you're interested in (like a JButton). It is these ActionListener implementations that will contain the logic, but keep in mind that if you don't use threading, your UI element (like the JButton) will not be responsive while your ActionListener is doing some calculations and holding control.

Shakedown
Good point, example code?
Rob
http://www.javapractices.com/topic/TopicAction.do?Id=153Check out this article - it shows a typical integration of Swing and logic, and also does it in a multithreaded way.
Shakedown
+2  A: 

I found this article How to Write Custom Swing Component helpful as an example of UI delegate plumbing.

Filthy Rich Clients by Chet Haase and Romain Guy is widely recommended for tips on visually rich client applications.

trashgod
+1 Thanks: I'll read the article and check out the book and follow up here...
Rob