0% found this document useful (0 votes)
5 views

Java Advanced Chapter Two

This document provides an overview of Java Swing, including its hierarchy, components, and differences from AWT. It covers various Swing components such as JButton, JTextField, and JLabel, along with examples of creating frames using association and inheritance. The document serves as a guide for understanding and implementing Java Swing in window-based applications.

Uploaded by

xafsocwali
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)
5 views

Java Advanced Chapter Two

This document provides an overview of Java Swing, including its hierarchy, components, and differences from AWT. It covers various Swing components such as JButton, JTextField, and JLabel, along with examples of creating frames using association and inheritance. The document serves as a guide for understanding and implementing Java Swing in window-based applications.

Uploaded by

xafsocwali
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/ 13

JAVA Advanced CHAPTER Two

Presenter:
Eng: Bishaar Abdisalam mohamed
Course Outline:
Introduction SWING
Java SWING Hierarchy
Difference between AWT and Swing
Jlabel
JTextField
JTextArea
JPasswordField
JCheckBox
JRadioButton
Jbutton
JComboBox
Java Swing

• Java Swing 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.

• Unlike AWT, 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.
Difference between AWT and Swing

• There are many differences between java AWT and swing that are
given below.

• Java AWT Java Swing


Java swing components are platform-
 AWT components are platform- independent.
dependent Swing components are lightweight
 AWT components are heavyweight. Swing provides more powerful
 AWT provides less components than components such as tables, lists, scrollpanes,
Swing. colorchooser, tabbedpane etc.
Hierarchy of Java Swing classes
Java Swing Examples

• There are two ways to create a frame:

• By creating the object of Frame class (association)

• By extending Frame class (inheritance)


Simple Java Swing Example
• Let's see a simple swing example where we are creating one button
and adding it on the JFrame object inside the main() method.

import javax.swing.*;
f.add(b);//adding button in JFrame
public class FirstSwingExample {
public static void main(String[] args) {
f.setSize(400,500);//400 width and 500 height
JFrame f=new JFrame
f.setLayout(null);//using no layout managers
JButton b=new JButton("click");
f.setVisible(true);//making the frame visible
//creating instance of JButton
}
b.setBounds(130,100,100, 40);//
}
x axis, y axis, width, height
Example of Swing by Association inside constructor

• We can also write all the codes of creating JFrame, JButton and
method call inside the java constructor.
import javax.swing.*;
public class Simple { f.setSize(400,500);//400 width and 500 height
JFrame f; f.setLayout(null);//using no layout managers
Simple(){ f.setVisible(true);//making the frame visible
f=new JFrame(); }
//creating instance of JFrame
JButton b=new JButton("click");// public static void main(String[] args) {
creating instance of JButton new Simple();
b.setBounds(130,100,100, 40); }
}
f.add(b);//adding button in JFrame
Simple example of Swing by inheritance

• We can also inherit the JFrame class, so there is no need to create the
instance of JFrame class explicitly.

add(b);//adding button on frame


setSize(400,500);
import javax.swing.*;
setLayout(null);
public class Simple2 extends JFrame{
setVisible(true);
Simple2(){
}
JButton b=new JButton("click");
public static void main(String[] args) {
b.setBounds(130,100,100, 40);
new Simple2();
}}
Java JLabel

• The object of JLabel class is a component for placing text in a


container. It is used to display a single line of read only text.
JTextField

• The object of a JTextField class is a text component that allows the


editing of a single line text. It inherits JTextComponent class.
JTextArea

• The object of a JTextArea class is a multi line region that displays text.
It allows the editing of multiple line text. It inherits JTextComponent
class
JPasswordField

• The object of a JPasswordField class is a text component specialized


for password entry. It allows the editing of a single line of text. It
inherits JTextField class.

You might also like