0% found this document useful (0 votes)
10 views34 pages

Swing Apple

Java Swing is a part of Java Foundation Classes (JFC) for creating window-based applications, built on AWT and providing lightweight, platform-independent components. It includes various components like JButton and JTextField, and supports features such as pluggable look and feel and MVC architecture. The document also covers layout managers, applet lifecycle methods, and provides code examples for creating Swing applications and applets.
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)
10 views34 pages

Swing Apple

Java Swing is a part of Java Foundation Classes (JFC) for creating window-based applications, built on AWT and providing lightweight, platform-independent components. It includes various components like JButton and JTextField, and supports features such as pluggable look and feel and MVC architecture. The document also covers layout managers, applet lifecycle methods, and provides code examples for creating Swing applications and applets.
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/ 34

Java Swing

• Java Swing tutorial 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.
Swing and AWT
No. Java AWT Java Swing
1) AWT components are platform-dependent. Java swing components are platform-independent.

2) AWT components are heavyweight. Swing components are lightweight.

3) AWT doesn't support pluggable look and feel. Swing supports pluggable look and feel.

4) AWT provides less components than Swing. Swing provides more powerful componentssuch as
tables, lists, scrollpanes, colorchooser, tabbedpane etc.

5) AWT doesn't follows MVC(Model View Controller) Swing follows MVC.


where model represents data, view represents
presentation and controller acts as an interface
between model and view.
Method Description

public void add(Component c) add a component on another


component.
public void setSize(int width,int sets size of the component.
height)
public void sets the layout manager for the
setLayout(LayoutManager m) component.
public void setVisible(boolean b) sets the visibility of the
component. It is by default false.
• There are two ways to create a frame:
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
}
}
Simple example of Swing by inheritance

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();
}}
Java JButton Example with ActionListener

import java.awt.event.*;
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
final JTextField tf=new JTextField();
tf.setBounds(50,50, 150,20);
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Javatpoint.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
• JComboBox cb=new JComboBox(country);

cb.setBounds(50, 50,90,20);
f.add(cb);
CheckBox checkBox1 = new JCheckBox("C+
+");
checkBox1.setBounds(100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox(
"Java", true);
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1);
f.add(checkBox2);
DefaultListModel<String> l1 = new DefaultList
Model<>();
l1.addElement("Item1");
l1.addElement("Item2");
l1.addElement("Item3");
l1.addElement("Item4");
JList<String> list = new JList<>(l1);
JMenuBar mb=new JMenuBar();
menu=new JMenu("Menu");
submenu=new JMenu("Sub Menu");
i1=new JMenuItem("Item 1");
i2=new JMenuItem("Item 2");
i3=new JMenuItem("Item 3");
i4=new JMenuItem("Item 4");
i5=new JMenuItem("Item 5");
menu.add(i1); menu.add(i2); menu.add(i3);
submenu.add(i4); submenu.add(i5);
menu.add(submenu);
mb.add(menu);
Java LayoutManagers

The LayoutManagers are used to arrange


components in a particular manner.
LayoutManager is an interface that is
implemented by all the classes of layout
managers. There are following classes that
represents the layout managers:
• java.awt.BorderLayout
• java.awt.FlowLayout
• java.awt.GridLayout
• java.awt.CardLayout
import java.awt.*;
import javax.swing.*;

public class MyGridLayout{


JFrame f;
MyGridLayout(){
f=new JFrame();

JButton b1=new JButton("1");


JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");

f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);
f.add(b6);f.add(b7);f.add(b8);f.add(b9);

f.setLayout(new GridLayout(3,3));
//setting grid layout of 3 rows and 3 columns

f.setSize(300,300);
f.setVisible(true);
Java Applet

• Applet is a special type of program that is


embedded in the webpage to generate the
dynamic content. It runs inside the browser
and works at client side.
• Advantage of Applet
 There are many advantages of applet. They are as
follows:
 It works at client side so less response time.
 Secured
 It can be executed by browsers running under many
plateforms, including Linux, Windows, Mac Os etc.
• Drawback of Applet
 Plugin is required at client browser to execute applet.
Lifecycle methods for Applet:
The java.applet.Applet class 4 life cycle methods and
java.awt.Component class provides 1 life cycle methods for an applet.
java.applet.Applet class
For creating any applet java.applet.Applet class must be inherited. It
provides 4 life cycle methods of applet.
public void init(): is used to initialized the Applet. It is invoked only once.
public void start(): is invoked after the init() method or browser is
maximized. It is used to start the Applet.
public void stop(): is used to stop the Applet. It is invoked when Applet is
stop or browser is minimized.
public void destroy(): is used to destroy the Applet. It is invoked only
once.
• How to run an Applet?
• There are two ways to run an applet
• By html file.
• By appletViewer tool (for testing purpose).
• import java.applet.Applet;
import java.awt.*;

public class GraphicsDemo extends Applet{

public void paint(Graphics g){


g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);

g.setColor(Color.pink);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);

}
}
• <html>
• <body>
• <applet code="GraphicsDemo.class" width="3
00" height="300">
• </applet>
• </body>
• </html>
import java.awt.*;
import javax.swing.*;

public class MyFlowLayout{


JFrame f;
MyFlowLayout(){
f=new JFrame();

JButton b1=new JButton("1");


JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");

f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);

f.setLayout(new FlowLayout(FlowLayout.RIGHT));
//setting flow layout of right alignment

f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new MyFlowLayout();
}
}
Applet with Event Handling
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class EventApplet extends Applet implements ActionListener{
Button b;
TextField tf;

public void init(){


tf=new TextField();
tf.setBounds(30,40,150,20);

b=new Button("Click");
b.setBounds(80,150,60,50);

add(b);add(tf);
b.addActionListener(this);

setLayout(null);
}

public void actionPerformed(ActionEvent e){


tf.setText("Welcome");
}
}
myapplet.html
<html>
<body>
<applet code="EventApplet.class" width="300"
height="300">
</applet>
</body>
</html>

You might also like