Intro to Swing

Swing
Swing is the primary Java GUI widget toolkit. It is part of Oracle's Java Foundation Classes (JFC) — an API for providing a graphical user interface (GUI) for Java programs.
Swing was developed to provide a more sophisticated set of GUI components than the earlier Abstract Window Toolkit (AWT). Swing provides a native look and feel that emulates the look and feel of several platforms, and also supports a pluggable look and feel that allows applications to have a look and feel unrelated to the underlying platform. It has more powerful and flexible components than AWT. In addition to familiar components such as buttons, check boxes and labels, Swing provides several advanced components such as tabbed panel, scroll panes, trees, tables, and lists.
Unlike AWT components, Swing components are not implemented by platform-specific code. Instead they are written entirely in Java and therefore are platform-independent. The term "lightweight" is used to describe such an element.
Swing component follows a Model-View-Controller architecture to fulfill the following criterias:-
* A single API is to be sufficient to support multiple look and feel.
* API is to model driven so that highest level API is not required to have the data.
* API is to use the Java Bean model so that Builder Tools and IDE can provide better services to the developers to use it.
MVC Architecture
Swing API architecture follows loosely based MVC architecture in the following manner.-
1. A Model represents component's data.
2. View represents visual representation of the component's data.
3. Controller takes the input from the user on the view and reflects the changes in Component's data.
4. Swing component have Model as a seperate element and View and Controller part are clubbed in User Interface elements. Using this way, Swing has pluggable look-and-feel architecture.
Swing features
1. Light Weight - Swing component are independent of native Operating System's API as Swing API controls are rendered mostly using pure JAVA code instead of underlying operating system calls.
2. Rich controls - Swing provides a rich set of advanced controls like Tree, TabbedPane, slider, colorpicker, table controls
3. Highly Customizable - Swing controls can be customized in very easy way as visual apperance is independent of internal representation.
4. Pluggable look-and-feel- SWING based GUI Application look and feel can be changed at run time based on available values.
Swing Controls
In Java, a component is the basic user interface object and is found in all Java applications. Components include lists, buttons, panels, and windows.
To use components, you need to place them in a container.
A container is a component that holds and manages other components. Containers display components using a layout manager. Swing components inherit from the javax.Swing.JComponent class, which is the root of the Swing component hierarchy.
JComponent, in turn, inherits from the Container class in the Abstract Windowing Toolkit (AWT). So Swing is based on classes inherited from AWT.
Swing provides the following useful top-level containers, all of which inherit from JComponent:
Every user interface considers the following three main aspects:
1. UI elements : Thes are the core visual elements the user eventually sees and interacts with. GWT provides a huge list of widely used and common elements varying from basic to complex which we will cover in this tutorial.
2. Layouts: They define how UI elements should be organized on the screen and provide a final look and feel to the GUI (Graphical User Interface). This part will be covered in Layout chapter.
3. Behavior: These are events which occur when the user interacts with UI elements. This part will be covered in Event Handling chapter.

Every SWING controls inherits properties from following Component class hiearchy.
Sno. Control & Description
1 JLabel
A JLabel object is a component for placing text in a container.
2 JButton
This class creates a labeled button.
3 JColorChooser
A JColorChooser provides a pane of controls designed to allow a user to manipulate and select a color.
4 JCheck Box
A JCheckBox is a graphical component that can be in either an on (true) or off (false) state.
5 JRadioButton
The JRadioButton class is a graphical component that can be in either an on (true) or off (false) state. in a group.
6 JList
A JList component presents the user with a scrolling list of text items.
7 JComboBox
A JComboBox component presents the user with a to show up menu of choices.
8 JTextField
A JTextField object is a text component that allows for the editing of a single line of text.
9 JPasswordField
A JPasswordField object is a text component specialized for password entry.
10 JTextArea
A JTextArea object is a text component that allows for the editing of a multiple lines of text.
11 ImageIcon
A ImageIcon control is an implementation of the Icon interface that paints Icons from Images
12 JScrollbar
A Scrollbar control represents a scroll bar component in order to enable user to select from range of values.
13 JOptionPane
JOptionPane provides set of standard dialog boxes that prompt users for a value or informs them of something.
14 JFileChooser
A JFileChooser control represents a dialog window from which the user can select a file.
15 JProgressBar
As the task progresses towards completion, the progress bar displays the task's percentage of completion.
16 JSlider
A JSlider lets the user graphically select a value by sliding a knob within a bounded interval.
17 JSpinner
A JSpinner is a single line input field that lets the user select a number or an object value from an ordered sequence.

JFrame and JPanel containers
1. JFrame: - The class JFrame is an extended version of java.awt.Frame that adds support for the JFC/Swing component architecture.
Example: -
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SwingContainerDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JLabel msglabel;
public SwingContainerDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingContainerDemo swingContainerDemo = new SwingContainerDemo();
swingContainerDemo.showJFrameDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
msglabel = new JLabel("Welcome to E-learning Education", JLabel.CENTER);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showJFrameDemo(){
headerLabel.setText("Container in action: JFrame");
final JFrame frame = new JFrame();
frame.setSize(300, 300);
frame.setLayout(new FlowLayout());
frame.add(msglabel);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
frame.dispose();
}
});
JButton okButton = new JButton("Open a Frame");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusLabel.setText("A Frame shown to the user.");
frame.setVisible(true);
}
});
controlPanel.add(okButton);
mainFrame.setVisible(true);
}
}
Output: -
C:\java\swing>javac SwingContainerDemo.java
C:\java\swing>java SwingContainerDemo
.
2. JPanel: -The class JPanel is a generic lightweight container.A JPanel is an empty area that can be used either to
1. Layout other components, including other panels.
2. Draw graphics on.
Example: -
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingContainerDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JLabel msglabel;
public SwingContainerDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingContainerDemo swingContainerDemo = new SwingContainerDemo();
swingContainerDemo.showJPanelDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
msglabel = new JLabel("Welcome to E-Learning Education", JLabel.CENTER);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showJPanelDemo(){
headerLabel.setText("Container in action: JPanel");
JPanel panel = new JPanel();
panel.setBackground(Color.blue);
panel.setLayout(new FlowLayout());
panel.add(msglabel);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
}
Output: -
C:\java\swing>javac SwingContainerDemo.java
C:\java\swing>java SwingContainerDemo
.
Awt & Swing Relationship
AWT Swing
AWT stands for Abstract windows toolkit. Swing is also called as JFC’s (Java Foundation classes).
AWT components are called Heavyweight component. Swings are called light weight component because swing components sits on the top of AWT components and do the work.
AWT components require java.awt package. Swing components require javax.swing package.
AWT components are platform dependent. Swing components are made in purely java and they are platform independent.
This feature is not supported in AWT. We can have different look and feel in Swing.
These feature is not available in AWT. Swing has many advanced features like JTabel, Jtabbed pane which is not available in AWT. Also. Swing components are called "lightweight" because they do not require a native OS object to implement their functionality. JDialog and JFrame are heavyweight, because they do have a peer. So components like JButton, JTextArea, etc., are lightweight because they do not have an OS peer.
With AWT, you have 21 "peers" (one for each control and one for the dialog itself). A "peer" is a widget provided by the operating system, such as a button object or an entry field object. With Swing, you would have only one peer, the operating system's window object. All of the buttons, entry fields, etc. are drawn by the Swing package on the drawing surface provided by the window object. This is the reason that Swing has more code. It has to draw the button or other control and implement its behavior instead of relying on the host operating system to perform those functions.
AWT is a thin layer of code on top of the OS. Swing is much larger. Swing also has very much richer functionality.
Using AWT, you have to implement a lot of things yourself. Swing has them built in.


Free Web Hosting