tags:

views:

174

answers:

6

Is there a resource where GUI design for swing is explained? Like best practices and such.

+3  A: 

I have written a list of recommendations here.

Adamski
+1  A: 

You can check the ideas behind FEST - a swing testing framework. It's main site is here and the project is hosted here

Bozho
+5  A: 

In larger swing projects I do partinioning of the app like that:

  • Have one class per GUI element like JPanel,JDialog etc.

  • Use a separate package for each screen, especially if you have to implement customized TableModels or other complex data structures

  • Don't use anonymous and inner classes, implement instead an ActionListener and check ActionEvent.getActionCommand() in there.

EDIT: If you're rather looking for a tutorial or introduction you could start here

stacker
+1 for no anonymous inner classes--those and closures are the death of code reuse in GUIs.
Bill K
Agreed, but the question seems to be about UI ergonomics and user experience.
Pascal Thivent
@Pascal Thivent thanks, than we should probably recommend swt?
stacker
@Stacker look at Apache Pivot instead of SWT. Apache Pivot is doing some very nice things as UI toolkit.
chubbard
+4  A: 

Maybe not exactly what your looking for but it won't hurt to take a peek at Java Look and Feel Design Guidelines

camickr
+1  A: 

You can find some of best practices in chapter 4 of Professional Java JDK6 Edition

alt text

Roman
+6  A: 

Design guidelines aren't exactly followed much anymore because Swing works on so many different platforms. Sun wrote up some a long time ago, and never changed them so I'd say read it if you think it will help. Here is some practical knowledge about doing swing development.

  • Never use GridBagLayout. Grab TableLayout. It radically simplifies layout for you Swing UI. GridBagLayout is the devil.
  • Don't over embed components just to get a layout right (i.e. embedded BoxLayout, etc). See point 1 for how to do this. There are performance issues having components on the screen.
  • Separate your program along MVC lines. Swing has View and Model separation, but in large programs the View (i.e. what subclasses a Swing Component) turns into a psuedo View/Controller only making things complicated to reuse and maintain. It turns into spaghetti code fast. Break the habit and create a Controller class that DOES NOT extend Swing. Same goes for the Model (no swing). Controller instantiates high level view classes, and wires itself as a listener to views.
  • Simplify popup dialogs using simple panels only. Don't subclass JDialog. Create a reusable dialog class that wraps a panel that can be used something like JOptionPane. Your panels will not be tied to dialogs only and can be reused. It's very easy when you work this way.
  • Avoid actionlistener/command. This is old junk and not very reusable. Use AbstractAction (anon classes are your choice I don't have a problem with them). AbstractAction encapsulates text, icon, mneumonics, accelerators, reusable in buttons, popups, menus, handles toggling enabled/disabled states, can be shared among multiple components, it also is the basis for InputMap/ActionMaps for mapping keyboard strokes to actions. ActionMaps give you loads of power for reuse.
  • It's best to have to view dispatch events to the controller. I'm not talking about mouse/keyboard junk, but high level events. Like NewUserEvent, AddUserEvent, DeleteUserEvent, etc. Have your controller listen for these high-level business events. This will promote encapsulation by keeping the concerns of the view (should I use a table, list, tree, or something else?) separated from the flow of the application. The controller doesn't care if the user clicked a button, menu, or checkbox.
  • Events are not just for the Controller. Swing is event programming. Your model will be doing things off the SwingThread or in the background. Dispatching events back to the controller is a very easy way to have it respond to things going on in the model layer that might be using threads to do work.
  • Understand Swing's Threading rules! You'd be surprised how few people actually understand that Swing is single threaded and what that means for multi-threaded applications.
  • Understand what SwingUtilities.invokeLater() does.
  • Never ever use SwingUtilities.invokeAndWait(). You're doing it wrong. Don't try and write synchronis code in event programming.
  • If you're starting a fresh project from scratch skip Swing. It's old, and it's over. Sun never really cared for the client like it did the server. Swing has been maintained poorly and not much progress has taken place since it was first written. JavaFX is not yet there, and suffers from lots of Swing's sins. I'd say look at Apache Pivot. Lots of new ideas and better design and an active community.
chubbard
Lots of good points. Thanks for the answerApache Pivot looks good. However, I don't like the idea of having an XML around for GUI. It seems cluttered.
tou
XML is only there for layout. And this is the approach take in both Flex and Silverlight. Think of this: in Swing we wrap Java around our layout and this makes it difficult to build WYSWYG editors. There are some out there but it's been slow to get what Visual Studio had for a decade before with resource files. So you still code in Java, but you layout your UI in XML. And because layout is in XML you can potentially use visual editors because XML is easier to parse than Java. It actually separates your code from the view.
chubbard
Good points, but I have to disagree with some.1. GridBagLayout is not devil. Devil is its use when unnecessary. It's complex layout, true, but it works well. 2. AbstractAction is great - but its use for every action component in ui can quickly lead to an unmaintainable mess. So I'd recommend the combination of both approaches. AbstractAction for the code that will be reused, and ActionListener for local use.
Taisin
Look at GridBagLayout this way. Why use a tool when there are better tools out there? TableLayout can do everything GridBagLayout can with far less code. You can literally turn pages of code using GridBag into 5 lines with TableLayout. Give it a try I promise you you'll never wish GridBagLayout on anyone again. I've yet to meet a Swing developer to prefer GridBagLayout to TableLayout after they've used TableLayout.
chubbard
The fact that Flex and Silverlight use XML doesn't mean that it is good! I also dislike XML for layout out GUIs. There are lots of Swing LayoutManager out there that give good layouts in pure Java. Check DesignGridLayout and MigLayout (there are also others).
jfpoilpret
GridBagLayout is quite bad but essentially because of the cluttered code it requires, otherwise it allows many kinds of designs, sometimes at the cost of having to embed another panel.There is a little nifty utility that wraps a fluent API around GBL, called "painless gridbag", quite neat!
jfpoilpret