Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

21-Java Swing

The document outlines the key differences between Java AWT and Java Swing, highlighting that AWT is platform-dependent and heavyweight while Swing is platform-independent and lightweight. It explains the MVC architecture used in Swing, where the model represents data, the view presents it, and the controller manages user interaction. Additionally, it describes the structure of Swing components and containers, along with example code demonstrating their use.

Uploaded by

TARINI MISHRA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

21-Java Swing

The document outlines the key differences between Java AWT and Java Swing, highlighting that AWT is platform-dependent and heavyweight while Swing is platform-independent and lightweight. It explains the MVC architecture used in Swing, where the model represents data, the view presents it, and the controller manages user interaction. Additionally, it describes the structure of Swing components and containers, along with example code demonstrating their use.

Uploaded by

TARINI MISHRA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Difference between AWT and Swing

Java AWT Java Swing


Java swing components are
1) AWT components are platform-dependent.
platform-independent.
2) AWT components are heavyweight. Swing components are lightweight.
Swing supports pluggable look and
3) AWT doesn't support pluggable look and feel.
feel.
Swing provides more powerful
components such as tables, lists,
4) AWT provides less components than Swing.
scrollpanes, colorchooser,
tabbedpane etc.
AWT doesn't follows MVC(Model View
Controller) where model represents data, view
5) Swing follows MVC.
represents presentation and controller acts as an
interface between model and view.

The MVC Connection (Model-View-Controller, or MVC


for short)
In MVC terminology, the model corresponds to the state information associated with the
component.

The view determines how the component is displayed on the screen, including any aspects of
the view that are affected by the current state of the model.

The controller determines how the component reacts to the user.

In general, a visual component is a composite of three distinct aspects:


• The way that the component looks when rendered on the screen
• The way that the component reacts to the user
• The state information associated with the component

Swing uses a modified version of MVC that combines the view and the controller into a single
logical entity called 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.

Components
Swing components are derived from the JComponent class.
JComponent provides the functionality that is common to all components.
JComponent inherits the AWT classes Container and Component.
Thus, a Swing component is built on and compatible with an AWT component.
All of Swing’s components are represented by classes defined within the package
javax.swing.
Containers
Swing defines two types of containers.
The first are top-level containers: JFrame, JApplet, JWindow, and JDialog.

The second type of containers supported by Swing are lightweight containers. Lightweight
containers do inherit JComponent. An example of a lightweight container is JPanel, which is
a general-purpose container.

The Top-Level Container Panes


Each top-level container defines a set of panes. At the top of the hierarchy is an instance of
JRootPane.

The Swing Packages


Example
import javax.swing.*;
class SwingDemo {
SwingDemo() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("A Simple Swing Application");
// Give the frame an initial size.
jfrm.setSize(275, 100);
//Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create a text-based label.
JLabel jlab = new JLabel(" Swing means powerful GUIs.");
//Add the label to the content pane.
jfrm.add(jlab);
//Display the frame.
jfrm.setVisible(true);
}
public static void main(String args[]) {
//Create the frame on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SwingDemo();
}
});
}

import javax.swing.*;
public class FirstSwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame

JButton b=new JButton("click");//creating instance of JButton


b.setBounds(130,100,100, 40);//x axis, y axis, width, height

f.add(b);//adding button in JFrame

f.setSize(400,500);//400 width and 500 height


f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
}
Event Handling
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(220, 90);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Make two buttons.
JButton jbtnAlpha = new JButton("Alpha");
JButton jbtnBeta = new JButton("Beta");
// Add action listener for Alpha.
jbtnAlpha.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jlab.setText("Alpha was pressed.");
}
});
// Add action listener for Beta.
jbtnBeta.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
jlab.setText("Beta was pressed.");
}
});
// Add the buttons to the content pane.
jfrm.add(jbtnAlpha);
jfrm.add(jbtnBeta);
// Create a text-based label.
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[]) {
//Create the frame on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new EventDemo();
}
});
}
}

You might also like