0% found this document useful (0 votes)
9 views14 pages

Presentation 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views14 pages

Presentation 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

In this model, a source generates

an event and forwards it to one or


more listeners. The listener waits
until it receives an event. Once it
receives the event, it is processed
by the listener and returns it. The
UI elements are able to delegate
the processing of an event to a
separate function.
• The key advantage of the Delegation Event Model is that the
application logic is completely separated from the interface logic.
• In this model, the listener must be connected with a source to receive
the event notifications. Thus, the events will only be received by the
listeners who wish to receive them. So, this approach is more
convenient than the inheritance-based event model
Java Adapter Classes

• Java adapter classes provide the default implementation of listener


interfaces.
• If you inherit the adapter class, you will not be forced to provide the
implementation of all the methods of listener interfaces. So it saves
code.
• The adapter classes are found in java.awt.event , java.awt.dnd and
javax.swing.event packages.
Java Swing
• It is a part of Java Foundation Classes (JFC) that is used to create
window-based applications.
• It is built on the top of AWT (Abstract Windowing Toolkit) API and
entirely written in java.
• Java Swing provides platform-independent and lightweight
components.
• The javax.swing package provides classes for java swing API such as
JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu,
JColorChooser etc.
Features of Swing

Swing provides many new features; two of its popular features are:
• Lightweight components
• Pluggable look and feel
Lightweight Components

• Swing components are lightweight as they are written entirely in Java


and do not depend on native peers (platform specific code resources).
• They use simple drawing primitives to render themselves on the
screen.
• The look and the feel of the component is not controlled by the
underlying operating system but by Swing itself.
Pluggable Look and Feel

•The pluggable look arid feel feature allows us to tailor the look and
feel of the application.
• We can even switch to different look and feel at runtime.
• Swing has the capability to support several look and feels, but at
present.
•As the look and feel of components is controlled by Swing rather than
by operating system, the feel of components can also be changed.
•The look and feel of a component can be separated form the logic of
the component. Thus, it is possible to “plug in” a new look and feel for
any given component without affecting the rest of the code.
Hierarchy of Java Swing classes
Components and Containers
• A component is an independent visual control, such as a push button or
slider.
• A container holds a group of components.
• A container is a special type of component that is designed to hold
other components.

• Swing components are derived from the JComponent class.


• Swing defines two types of containers:
• The first are top-level containers: JFrame, JApplet, JWindow, and JDialog. These
containers do not inherit JComponent.
• 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.
Java Swing Examples

• By creating the object of Frame class (association)


• By extending Frame class (inheritance)
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
}
}
Example of Swing by Association inside constructor
import javax.swing.*;
public class Simple {
JFrame f;
Simple(){
f=new JFrame();//creating instance of JFrame
JButton b=new JButton("click");//creating instance of JButton
b.setBounds(130,100,100, 40);
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
}

public static void main(String[] args) {


new Simple();
}
import javax.swing.*;
public class Simple2 extends JFrame{//inheriting JFrame
JFrame f;
Simple2(){
JButton b=new JButton("click");//create button
b.setBounds(130,100,100, 40);

add(b);//adding button on frame


setSize(400,500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new Simple2();
}
}

You might also like