Monday 26 March 2012

Components and Containers in Swing

•In general every Swing GUI consists of “components” and “containers”.
•All containers are components.
•A component is a thing/object that has a visual appearance and on which user can perform events.
•A container is a object/thing which can hold other components.
•Since all containers are components, a container can hold other containers.
•Every component must be added to a container in order to display it.
•So, every Swing GUI will contain atleast one container.
•This allows for container hierarchy, in which at the top we will have “top-level containers”.

•All the Swing components are derived from the “JComponent” class except the four top level containers: JFrame, JApplet, JWindow and JDialog.
•The above four top level containers are the only heavy weight components in the Swing package.
•“JComponent“ inherits both “Container” and “Component” in AWT package.
•So, all the Swing components are built on top of the AWT components.
•The four top level containers: JFrame, JApplet, JWindow and JDialog do not inherit “JComponent“ class.
•The are two types of containers in Swing. They are:
•Top-level containers (JFrame, JApplet, JWindow and JDialog)
•Light weight containers (JPanel)


•Following are some of the examples of Swing components:
1.JLabel
2.JButton
3.JToggleButton
4.JCheckBox
5.JRadioButton
6.JComboBox
7.JList
8.JTextField
9.JScrollPane
10.JTabbedPane
11.JTree
12.JTable

MVC Architecture

•Generally each visual component consists of three distinct aspects:
1.The state information associated with the component.
2.The visual appearance of the component.
3.The way the component reacts to the user.
•In MVC(Model-View-Controller) architecture “model” corresponds to the state information of the component. “view” corresponds to the visual appearance of the component and the “controller” corresponds to how the component reacts to the user.
•In MVC architecture, model, view and controller are independent of one another.

•It means that the state information is independent of the look and feel of the component.
•Swing component uses a modified version of the MVC architecture known as “Model-Delegate” or “Separate model” architecture.
•In Java, “view” and “controller” of the MVC had been combined into a single entity known as “UI Delegate”.

Introduction to Swing


•Swing package provides classes which are an extension of the classes defined in the AWT package.
•Swing components are more powerful and flexible than the components available in AWT package.
•The components in Swing package will have a modern look and feel in contrast to the AWT components.
•Swing components are used more over the AWT components due to three reasons:
1.The AWT components are translated into native components based on the operating system. They do not have the same look and feel in every operating system.
2.The look and feel of the AWT components is fixed.
3.AWT components are heavy weight components. They are rectangular in shape and opaque.

•Swing is built on AWT. Swing is not a replacement for AWT.
•Swing package provides two important features:
1.Swing components are light weight components.
2.Swing supports pluggable look and feel.
•Java SE 6 provides to look and feels: Metal and Motif.
•Metal is the default look and feel of Swing components. Metal is a platform independent look and feel.