Swing

What is the difference between AWT and Swing?

The biggest difference between the AWT components and Swing components is that the Swing components are implemented with absolutely no native code. They are platform independent.

Swing components provide much more features than what the AWT components offer:

  • More complex components, like JTree, JTable, JFileChooser.
  • Swing buttons and labels can display images instead of, or in addition to, text. Swing components don't have to be rectangular. Buttons, for example, can be round.
  • New containment model to maintain component hierarchy. You can easily change the behavior or appearance of a Swing component by either invoking methods on it or creating a subclass of it.
  • A new layout manage, BoxLayout manager.
  • Swing can let you specify which look and feel your program's GUI uses. By contrast, AWT components always have the look and feel of the native platform.
  • Another interesting feature is that Swing components with state use models to keep the state. A JSlider, for instance, uses a BoundedRangeModel object to hold its current value and range of legal values. Models are set up automatically, so you don't have to deal with them unless you want to take advantage of the power they can give you.

What are the heavyweight and lightweight components?

The AWT control components provide their graphical representation by using native peer components. The peer will be responsible for the look and feel of that particular component. These controls are heavyweight. When the components are not supported by a native peer, they are lightweight components. The difference between lightweight and heavyweight components is z-order: the notion of depth or layering. When Swing components (and all other "lightweight" components) overlap with heavyweight components, the heavyweight component is always painted on top.

The Pane Container Model

Swing top-level containers include JFrame, JDialog, JWindow and JApplet. There is a JRootPane class under them to rpovide more facilities: a content pane, a layered pane, a glass pane and menu-bar. The following methods help you getthem respectively.

   Container getContentPane();
   Component getGlassPane();
   JLayeredPane getLayeredPane();

Components can not be added directly to a top-level container such as a JFrame. Instead, they should be added to a content pane that is itself contained by the JFrame.

MVC architecture

The model is responsible for maintaining all aspects of the component state. Instead the model will send out notifications or broadcasts (what we know as events). The view determines the visual representation of the component's model. The view is responsible for keeping its on-screen representation updated and may do so upon receiving indirect messages from the model, or direct messages from the controller. The controller is responsible for determining whether the component should react to any input events from input devices such as the keyboard or mouse. It determines what actions are performed when the component is used.