ORIGIN OF SWING
- The AWT defines a basic set of controls, windows, and dialog boxes that support a usable, but limited, graphical interface.
- The solution was Swing. Introduced in 1997,
- Swing was included as part of the Java Foundation Classes (JFC).
- Swing was initially available for use with Java 1.1 as a separate library.
- However, beginning with Java 1.2, Swing (and the rest of the JFC) was fully integrated into Java.
- The Java Swing tutorial is part of the Java Foundation Classes (JFC), which is used to create window-based applications.
- It is built on top of the AWT (Abstract Windowing Toolkit) API and entirely written in Java.
- The javax.swing package provides classes for java swing API, such as JButton, JTextField, JTextArea, JRadioButton, JCheckBox, JMenu, JColorChooser, etc.
FEATURES OF SWINGS
- Lightweight Components:
- The components created using the swings package don’t need the help of the native operating system.
- Swings is part of Java Foundation Classes (JFC), which are purely developed in Java.
- Swing components require fewer memory and CPU cycles than AWT components.
- Hence, swing components are lightweight components.
- Pluggable Look and Feel:
- Swings supports pluggable look and feel, i.e., the appearance of a component can be separated from the component's behaviour.
- This enables programmers to assign different looks (themes) for the same component without changing its behaviour.
SWING VS AWT
- Every GUI control or component contains three aspects:
- The way the component looks (appearance) when rendered on the screen.
- The way the component reacts to the user.
- The state information associated with the component.
- In MVC terminology, the model corresponds to the state information of a component.
- For example, a button might contain a field that represents whether it is pressed or released.
- The view corresponds to the look or appearance of the component when rendered on the screen.
- For example, a button is displayed as a rectangle.
- The controller determines how the component reacts to the user events.
- For example, when the user presses a button, a new frame can be displayed.
MODEL-DELEGATE ARCHITECTURE
- Swing uses a modified version of MVC that combines the view and the controller into a single logical entity called the UI delegate.
- For this reason, Swing’s approach is called either the Model-Delegate architecture or the Separable Model architecture.
- Swing’s pluggable look and feel is made possible by its Model-Delegate architecture.
- To support the Model-Delegate architecture, most Swing components contain two objects.
- The first represents the model.
- The second represents the UI delegate.
COMPONENTS AND CONTAINERS
- A Swing GUI consists of two key items: components and containers.
- A component is an independent visual control, such as a push button or slider.
- A container holds a group of components.
- Thus, a container is a special type of component designed to hold other components.
- For a component to be displayed, it must be held within a Container.
- Thus, all Swing GUIs will have at least one container
COMPONENTS
- Swing components are derived from the class JComponent (except the four top-level containers).
- JComponent supports pluggable look and feel and supports the functionality common to all components.
- JComponent class inherits the AWT classes Container and Component.
- All of Swing’s components are represented by classes defined within the package javax.swing.
COMPONENTS IN SWINGS
CONTAINERS
- Swing defines two types of containers.
- The first are top-level containers: JFrame, JApplet, JWindow, and JDialog.
- The top-level containers are heavyweight since they are inherited from the AWT classes Component and Container.
- A top-level container is not contained within any other container.
- The one most commonly used for applications is JFrame.
- The second type of containers supported by Swing are lightweight containers.
- Lightweight containers do inherit JComponent.
- An example of a lightweight container is JPanel
- In the past, the one used for applets was JApplet.
EVENT HANDLING IN SWINGS
- The event handling mechanism used by Swing is the same as that used by the AWT.
- This approach is called the delegation event model.
- Swing uses the same events as does the AWT, and these events are packaged in java.awt.event.
- Events specific to Swing are stored in javax.swing.event.
PAINTING IN SWING
- To write output directly to the surface of a component, one or more drawing methods defined by the AWT, such as drawLine( ) or drawRect( ) must be used.
- The AWT class Component defines a method called paint( ) that is used to draw output directly to the surface of a component.
- Because JComponent inherits Component, all Swing’s lightweight components inherit the paint( ) method.
- Swing uses three distinct methods: paintComponent( ), paintBorder( ), and paintChildren( ).
- These methods paint the indicated portion of a component and divide the painting process into its three distinct, logical actions.
- To paint to the surface of a Swing component, create a subclass of the component class and then override its paintComponent( ) method.
Simple Swing Program
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class EventDemo
{
JLabel jlab;
EventDemo()
{
// Create a new JFrame container.
JFrame jfrm = new JFrame("An Event Example");
// Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(720, 690);
// Terminate the program when the close button is pressed
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Make three buttons.
JButton b1 = new JButton("RED");
JButton b2 = new JButton("BLUE");
JButton b3 = new JButton("GREEN");
// Add action listener for red.
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
jfrm.getContentPane().setBackground(Color.red);
}
});
// Add action listener for blue.
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
jfrm.getContentPane().setBackground(Color.blue);
}
});
//Add action listener for green
b3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
jfrm.getContentPane().setBackground(Color.green);
}
});
// Add the buttons to the content pane.
jfrm.add(b1);
jfrm.add(b2);
jfrm.add(b3);
// Create a text-based label.
JLabel jlab = new JLabel("Press a button.");
// Add the label to the content pane.
jfrm.add(jlab);
// Display the frame.
jfrm.setVisible(true);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new EventDemo();
}
});
}
}
No comments:
Post a Comment