Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are your best Swing design patterns and tips? [closed]

Never derive from JDialog, JFrame or JInternalFrame for defining your forms, dialogs...

Rather derive from JPanel. This will bring you the follwing advantages:

  • possibility to later change from a JFrame to a JDialog for instance (because user changed his mind)
  • you can reuse one panel instance from one JDialog to another (JDialog are generally not reusable because they are constructed with a reference to their "parent", a frame or another dialog)
  • you can later on change replace JDialog with a more functional subclass from a 3rd-party framework.

Use layout managers. You might think it's simpler just to position everything with hard coded positions now (especially if you use a graphical layout tool), but when it comes time to update the gui, or internationalize it, your successors will hate you. (Trust me on this, I was the guy saying to use the layout managers from the start, and the successor to the guy who ignored me.)


Avoid using GUI layout designers (builders). Later on it will make your code much cleaner and easier to maintain.


I think a good working knowledge of concurrency is often understated. You really need to be familiar with Swing's threading policy and general synchronization techniques to build a responsive GUI and an efficient backend.