SlideShare a Scribd company logo
1 of 23
Java Swing
Introduction
 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.
 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
AWT
 AWT components are platform-dependent.
 AWT components are heavyweight.
 AWT doesn't support pluggable look and feel.
 AWT provides less components than Swing.
 AWT doesn't follows MVC(Model View
Controller) where model represents data, view
represents presentation and controller acts as an
interface between model and view.
Contd..
Swing
 Java swing components are platform-
independent.
 Swing components are lightweight.
 Swing supports pluggable look and feel.
 Swing provides more powerful components
such as tables, lists, scrollpanes, colorchooser,
tabbedpane etc.
 Swing follows MVC.
Hierarchy of Java Swing classes
MVC
Simple Java Swing Example
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
}
}
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();
}}
JButton
 The JButton class is used to create a labeled
button that has platform independent
implementation.
 The application result in some action when the
button is pushed. It inherits AbstractButton class.
 JButton() -- It creates a button with no text and
icon.
JButton(String s)-- It creates a button with the
specified text.
JButton(Icon i) -- It creates a button with the
specified icon object.
import javax.swing.*;
public class ButtonExample{
ButtonExample(){
JFrame f=new JFrame("Button Example");
JButton b=new JButton(new ImageIcon("D:img.png"));
b.setBounds(100,100,100, 40);
f.add(b);
f.setSize(300,400);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ButtonExample();
}
}
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);
}
}
JOptionPane
 The JOptionPane class is used to provide standard
dialog boxes such as message dialog box, confirm
dialog box and input dialog box.
 These dialog boxes are used to display information or
get input from the user. The JOptionPane class
inherits JComponent class.
 JOptionPane() -It is used to create a JOptionPane
with a test message.
 JOptionPane(Object message) -It is used to create an
instance of JOptionPane to display a message.
 JOptionPane(Object message, int messageType )-It is
used to create an instance of JOptionPane to display
a message with specified message type and default
options.
Java Swing Layouts
 In Java swing, Layout manager is used to position all
its components, with setting properties, such as the
size, the shape, and the arrangement.
The following layout managers are:
 FlowLayout
 BorderLayout
 CardLayout
 BoxLayout
 GridLayout
 GridBagLayout
 GroupLayout
 SpringLayout
FlowLayout
The FlowLayout arranges the components in a
directional flow, either from left to right or from right to
left. Normally all components are set to one row,
according to the order of different components. If all
components can not be fit into one row, it will start a
new row and fit the rest in.
To construct a FlowLayout, three options could be
chosen:
 FlowLayout(): construct a new FlowLayout object with
center alignment and horizontal and vertical gap to
be default size of 5 pixels.
 FlowLayout(int align): construct similar object with
different settings on alignment
 FlowLayout(int align, int hgap, int vgap): construct
similar object with different settings on alignment and
gaps between components.
BorderLayout
A BorderLayout lays out a container, arranging its
components to fit into five regions: NORTH,
SOUTH, EAST, WEST and CENTER. For each
region, it may contain no more than one
component.
For BorderLayout, it can be constructed like below:
 BorderLayout(): construct a border layout with no
gaps between components.
 BorderLayout(int hgap, int vgap): construct a
border layout with specified gaps between
components.
CardLayout
For CardLayout, it treats the components as a stack and
every time, what you can see is only one component.
BoxLayout
BoxLayout with two different axis options: X_AXIS and
Y_AXIS. In X_AXIS, components are laid out horizontally
from left to right, while in Y_AXIS vertically from top to
bottom.
GridLayout
 The GridLayout manager is used to lay out the
components in a rectangle grid, which has been divided
into equal-sized rectangles and one component is placed
in each rectangle. It can constructed with following
methods:
 GridLayout(): construct a grid layout with one column per
component in a single row.
 GridLayout(int row, int col): construct a grid layout with
specified numbers of rows and columns.
 GridLayout(int row, int col, int hgap, int vgap): construct a
grid layout with specified rows, columns and gaps between
components.
Java JTabbedPane
The JTabbedPane class is used to switch between
a group of components by clicking on a tab with a
given title or icon. It inherits JComponent class.
JTabbedPane() Creates an empty TabbedPane
with a default tab placement of
JTabbedPane.Top.
JTabbedPane(int tabPlacement) Creates an
empty TabbedPane with a specified tab
placement.
JTabbedPane(int tabPlacement, int
tabLayoutPolicy) Creates an empty TabbedPane
with a specified tab placement and tab layout
policy.
JTree
JTree class is used to display the tree structured data or
hierarchical data. JTree is a complex component. It has a
'root node' at the top most which is a parent for all
nodes in the tree. It inherits JComponent class.
JTree() Creates a JTree with a sample model.
JTree(Object[] value) Creates a JTree with every
element of the specified array as the child of a new root
node.
JTree(TreeNode root) Creates a JTree with the specified
TreeNode as its root, which displays the root node.
Mnemonics and Accelerators to
Menu Items
 Menus support two kinds of keyboard
alternatives: mnemonics and accelerators.
 Mnemonics offer a way to use the keyboard to
navigate the menu hierarchy, increasing the
accessibility of programs.
 Accelerators, on the other hand, offer keyboard
shortcuts to bypass navigating the menu
hierarchy.
 The menu created in the preceding example is
functional, but it is possible to make it better.
 In real applications, a menu usually includes
support for keyboard shortcuts because they give
an experienced user the ability to select menu
items rapidly.
 Keyboard shortcuts come in two forms:
mnemonics and accelerators. As it applies to
menus, a mnemonic defines a key that lets you
select an item from an active menu by typing the
key.
 Thus, a mnemonic allows you to use the
keyboard to select an item from a menu that is
already being displayed.
 An accelerator is a key that lets you select a
menu item without having to first activate the
 To specify a mnemonic for JMenu, you must call
setMnemonic( ). This method is inherited by both
classes from AbstractButton and is shown next:
 void setMnemonic(int mnem)

More Related Content

What's hot (20)

Java Swing
Java SwingJava Swing
Java Swing
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
Event handling
Event handlingEvent handling
Event handling
 
Java swing
Java swingJava swing
Java swing
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
14 file handling
14 file handling14 file handling
14 file handling
 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing ButtonsJAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
 
Files in java
Files in javaFiles in java
Files in java
 
Java layoutmanager
Java layoutmanagerJava layoutmanager
Java layoutmanager
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Jsp
JspJsp
Jsp
 
Awt
AwtAwt
Awt
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
 
Graphics and Java 2D
Graphics and Java 2DGraphics and Java 2D
Graphics and Java 2D
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 

Similar to Java swing

Z blue introduction to gui (39023299)
Z blue   introduction to gui (39023299)Z blue   introduction to gui (39023299)
Z blue introduction to gui (39023299)Narayana Swamy
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892renuka gavli
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managersswapnac12
 
SWING USING JAVA WITH VARIOUS COMPONENTS
SWING USING  JAVA WITH VARIOUS COMPONENTSSWING USING  JAVA WITH VARIOUS COMPONENTS
SWING USING JAVA WITH VARIOUS COMPONENTSbharathiv53
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Javayht4ever
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingPayal Dungarwal
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Javasuraj pandey
 
DSJ_Unit III.pdf
DSJ_Unit III.pdfDSJ_Unit III.pdf
DSJ_Unit III.pdfArumugam90
 
Ajp notes-chapter-02
Ajp notes-chapter-02Ajp notes-chapter-02
Ajp notes-chapter-02Ankit Dubey
 
Getting started with GUI programming in Java_1
Getting started with GUI programming in Java_1Getting started with GUI programming in Java_1
Getting started with GUI programming in Java_1Muhammad Shebl Farag
 
Graphical User Interface in JAVA
Graphical User Interface in JAVAGraphical User Interface in JAVA
Graphical User Interface in JAVAsuraj pandey
 
Computer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptxComputer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptxjonathancapitulo2
 

Similar to Java swing (20)

Z blue introduction to gui (39023299)
Z blue   introduction to gui (39023299)Z blue   introduction to gui (39023299)
Z blue introduction to gui (39023299)
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
 
13457272.ppt
13457272.ppt13457272.ppt
13457272.ppt
 
SWING USING JAVA WITH VARIOUS COMPONENTS
SWING USING  JAVA WITH VARIOUS COMPONENTSSWING USING  JAVA WITH VARIOUS COMPONENTS
SWING USING JAVA WITH VARIOUS COMPONENTS
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.Swing
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
 
DSJ_Unit III.pdf
DSJ_Unit III.pdfDSJ_Unit III.pdf
DSJ_Unit III.pdf
 
Ingles 2do parcial
Ingles   2do parcialIngles   2do parcial
Ingles 2do parcial
 
Jp notes
Jp notesJp notes
Jp notes
 
Swing
SwingSwing
Swing
 
Swing
SwingSwing
Swing
 
Chap1 1 1
Chap1 1 1Chap1 1 1
Chap1 1 1
 
Chap1 1.1
Chap1 1.1Chap1 1.1
Chap1 1.1
 
swings.pptx
swings.pptxswings.pptx
swings.pptx
 
Ajp notes-chapter-02
Ajp notes-chapter-02Ajp notes-chapter-02
Ajp notes-chapter-02
 
Getting started with GUI programming in Java_1
Getting started with GUI programming in Java_1Getting started with GUI programming in Java_1
Getting started with GUI programming in Java_1
 
Graphical User Interface in JAVA
Graphical User Interface in JAVAGraphical User Interface in JAVA
Graphical User Interface in JAVA
 
Computer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptxComputer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptx
 

More from ssuser3a47cb

Uunit 5-xml&web security
Uunit 5-xml&web securityUunit 5-xml&web security
Uunit 5-xml&web securityssuser3a47cb
 
Unit 4-SOA governance
Unit 4-SOA governanceUnit 4-SOA governance
Unit 4-SOA governancessuser3a47cb
 
Unit 3-SOA Technologies
Unit 3-SOA TechnologiesUnit 3-SOA Technologies
Unit 3-SOA Technologiesssuser3a47cb
 
Unit 2 -SOA design
Unit 2 -SOA designUnit 2 -SOA design
Unit 2 -SOA designssuser3a47cb
 
I/O port programming in 8051
I/O port programming in 8051I/O port programming in 8051
I/O port programming in 8051ssuser3a47cb
 
Interfacing external memory in 8051
Interfacing external memory in 8051Interfacing external memory in 8051
Interfacing external memory in 8051ssuser3a47cb
 

More from ssuser3a47cb (10)

BCT.pptx
BCT.pptxBCT.pptx
BCT.pptx
 
Uunit 5-xml&web security
Uunit 5-xml&web securityUunit 5-xml&web security
Uunit 5-xml&web security
 
Unit 4-SOA governance
Unit 4-SOA governanceUnit 4-SOA governance
Unit 4-SOA governance
 
Unit 3-SOA Technologies
Unit 3-SOA TechnologiesUnit 3-SOA Technologies
Unit 3-SOA Technologies
 
Unit 2 -SOA design
Unit 2 -SOA designUnit 2 -SOA design
Unit 2 -SOA design
 
Soa 1 7.ppsx
Soa 1 7.ppsxSoa 1 7.ppsx
Soa 1 7.ppsx
 
Java networking
Java networkingJava networking
Java networking
 
I/O port programming in 8051
I/O port programming in 8051I/O port programming in 8051
I/O port programming in 8051
 
Interfacing external memory in 8051
Interfacing external memory in 8051Interfacing external memory in 8051
Interfacing external memory in 8051
 
Interrupt in 8051
Interrupt in 8051Interrupt in 8051
Interrupt in 8051
 

Recently uploaded

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...Call Girls in Nagpur High Profile
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 

Recently uploaded (20)

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 

Java swing

  • 2. Introduction  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.  The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
  • 3. Difference between AWT and Swing AWT  AWT components are platform-dependent.  AWT components are heavyweight.  AWT doesn't support pluggable look and feel.  AWT provides less components than Swing.  AWT doesn't follows MVC(Model View Controller) where model represents data, view represents presentation and controller acts as an interface between model and view.
  • 4. Contd.. Swing  Java swing components are platform- independent.  Swing components are lightweight.  Swing supports pluggable look and feel.  Swing provides more powerful components such as tables, lists, scrollpanes, colorchooser, tabbedpane etc.  Swing follows MVC.
  • 5. Hierarchy of Java Swing classes
  • 6. MVC
  • 7.
  • 8. Simple Java Swing Example 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 } }
  • 9.
  • 10. 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(); }}
  • 11. JButton  The JButton class is used to create a labeled button that has platform independent implementation.  The application result in some action when the button is pushed. It inherits AbstractButton class.  JButton() -- It creates a button with no text and icon. JButton(String s)-- It creates a button with the specified text. JButton(Icon i) -- It creates a button with the specified icon object.
  • 12. import javax.swing.*; public class ButtonExample{ ButtonExample(){ JFrame f=new JFrame("Button Example"); JButton b=new JButton(new ImageIcon("D:img.png")); b.setBounds(100,100,100, 40); f.add(b); f.setSize(300,400); f.setLayout(null); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new ButtonExample(); } }
  • 13. 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); } }
  • 14. JOptionPane  The JOptionPane class is used to provide standard dialog boxes such as message dialog box, confirm dialog box and input dialog box.  These dialog boxes are used to display information or get input from the user. The JOptionPane class inherits JComponent class.  JOptionPane() -It is used to create a JOptionPane with a test message.  JOptionPane(Object message) -It is used to create an instance of JOptionPane to display a message.  JOptionPane(Object message, int messageType )-It is used to create an instance of JOptionPane to display a message with specified message type and default options.
  • 15. Java Swing Layouts  In Java swing, Layout manager is used to position all its components, with setting properties, such as the size, the shape, and the arrangement. The following layout managers are:  FlowLayout  BorderLayout  CardLayout  BoxLayout  GridLayout  GridBagLayout  GroupLayout  SpringLayout
  • 16. FlowLayout The FlowLayout arranges the components in a directional flow, either from left to right or from right to left. Normally all components are set to one row, according to the order of different components. If all components can not be fit into one row, it will start a new row and fit the rest in. To construct a FlowLayout, three options could be chosen:  FlowLayout(): construct a new FlowLayout object with center alignment and horizontal and vertical gap to be default size of 5 pixels.  FlowLayout(int align): construct similar object with different settings on alignment  FlowLayout(int align, int hgap, int vgap): construct similar object with different settings on alignment and gaps between components.
  • 17. BorderLayout A BorderLayout lays out a container, arranging its components to fit into five regions: NORTH, SOUTH, EAST, WEST and CENTER. For each region, it may contain no more than one component. For BorderLayout, it can be constructed like below:  BorderLayout(): construct a border layout with no gaps between components.  BorderLayout(int hgap, int vgap): construct a border layout with specified gaps between components.
  • 18. CardLayout For CardLayout, it treats the components as a stack and every time, what you can see is only one component. BoxLayout BoxLayout with two different axis options: X_AXIS and Y_AXIS. In X_AXIS, components are laid out horizontally from left to right, while in Y_AXIS vertically from top to bottom. GridLayout  The GridLayout manager is used to lay out the components in a rectangle grid, which has been divided into equal-sized rectangles and one component is placed in each rectangle. It can constructed with following methods:  GridLayout(): construct a grid layout with one column per component in a single row.  GridLayout(int row, int col): construct a grid layout with specified numbers of rows and columns.  GridLayout(int row, int col, int hgap, int vgap): construct a grid layout with specified rows, columns and gaps between components.
  • 19. Java JTabbedPane The JTabbedPane class is used to switch between a group of components by clicking on a tab with a given title or icon. It inherits JComponent class. JTabbedPane() Creates an empty TabbedPane with a default tab placement of JTabbedPane.Top. JTabbedPane(int tabPlacement) Creates an empty TabbedPane with a specified tab placement. JTabbedPane(int tabPlacement, int tabLayoutPolicy) Creates an empty TabbedPane with a specified tab placement and tab layout policy.
  • 20. JTree JTree class is used to display the tree structured data or hierarchical data. JTree is a complex component. It has a 'root node' at the top most which is a parent for all nodes in the tree. It inherits JComponent class. JTree() Creates a JTree with a sample model. JTree(Object[] value) Creates a JTree with every element of the specified array as the child of a new root node. JTree(TreeNode root) Creates a JTree with the specified TreeNode as its root, which displays the root node.
  • 21. Mnemonics and Accelerators to Menu Items  Menus support two kinds of keyboard alternatives: mnemonics and accelerators.  Mnemonics offer a way to use the keyboard to navigate the menu hierarchy, increasing the accessibility of programs.  Accelerators, on the other hand, offer keyboard shortcuts to bypass navigating the menu hierarchy.
  • 22.  The menu created in the preceding example is functional, but it is possible to make it better.  In real applications, a menu usually includes support for keyboard shortcuts because they give an experienced user the ability to select menu items rapidly.  Keyboard shortcuts come in two forms: mnemonics and accelerators. As it applies to menus, a mnemonic defines a key that lets you select an item from an active menu by typing the key.  Thus, a mnemonic allows you to use the keyboard to select an item from a menu that is already being displayed.  An accelerator is a key that lets you select a menu item without having to first activate the
  • 23.  To specify a mnemonic for JMenu, you must call setMnemonic( ). This method is inherited by both classes from AbstractButton and is shown next:  void setMnemonic(int mnem)