SlideShare a Scribd company logo
1 of 19
By :-
Mrs. Jyoti Totla
Assistant Professor(CSE)
Mewar University
Swing
 Swing library is an official Java GUI toolkit released by
Sun Microsystems.
 Swing is probably the most advanced toolkit on this
planet.
 It has a rich set of widgets.
 From basic widgets like Buttons, Labels, Scrollbars to
advanced widgets like Trees and Tables.
 Swing is written in 100% java.
 Swing is a part of JFC, Java Foundation Classes. It is a
collection of packages for creating full featured
desktop applications.
SWT library
 There is also another GUI library for the Java
programming language. It is called SWT.
The Standard widget toolkit.
The SWT library was initially developed by the IBM
corporation.
 Now it is an open source project, supported by IBM.
The SWT is an example of a heavyweight toolkit. It lets
the underlying OS to create GUI. SWT uses the java
native interface to do the job.
 The main advantages of the SWT are speed and native
look and feel.
 The SWT is on the other hand more error prone. It is
less powerful then Swing. It is also quite Windows
centric library.
Basic window.
import javax.swing.JFrame;
public class Simple extends JFrame {
public Simple() {
setSize(300, 200);
setTitle("Simple JFrame");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Simple simple = new Simple();
simple.setVisible(true);
}
}
 import javax.swing.JFrame;
 Here we import the JFrame widget. It is a top level
container, which is used for placing other widgets.
 setSize(300, 200);
 setTitle("Simple");
 This code will resize the window to be 300px wide and
200px tall. It will set the title of the
 window to Simple.
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 This method will close the window, if we click on the
close button. By default nothing
 happens.
Set Image icon for Frame:-
 ImageIcon icon= new ImageIcon("1.jpg");
 f.setIconImage(icon.getImage());
Keep the frame window in center:
As per Screen Resolution.:-
Package:-java.awt.Toolkit;
java.awt.Dimension;
Toolkit toolkit = getToolkit();
Dimension size = toolkit.getScreenSize();
setLocation(size.width/2 - getWidth()/2,
size.height/2 - getHeight()/2);
}
Jpanel:-javax.swing.JPanel;
JPanel panel = new JPanel();
getContentPane().add(panel);
We create a JPanel component. We add the JPanel to
the JFrame.
By default, the JPanel has a FlowLayout manager. The
layout manager is used to place widgets onto the
containers. If we call setLayout(null) we can position
our components. absolutely. For this, we use the
setBounds() method.
panel.setLayout(null);
JButtons:-
package: javax.swing.JButton;
JButton beep = new JButton("Beep");
beep.setBounds(150, 60, 80, 30);
beep.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
toolkit.beep();
//System.exit(0);
 }
 });
A tooltip
 Tooltips are part of the internal application's help
system. The Swing shows a small rectangular window,
if we hover a mouse pointer over an object.
 button.setToolTipText("A button component");
MenuBar:-
 A menubar is one of the most visible parts of the GUI
application. It is a group of commands located in
various menus. While in console applications you had
to remember all those arcane commands, here we have
most of the commands grouped into logical parts.
There are accepted standards that further reduce the
amount of time spending to learn a new application.
 In Java Swing, to implement a menubar, we use three
objects. A JMenuBar, a JMenu and
 a JMenuItem.
Packages used:-
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
file.setMnemonic(KeyEvent.VK_F);
javax.swing.JCheckBoxMenuItem;
ImageIcon:-
ImageIcon icon = new ImageIcon("exit.png");
JMenuItem Close = new JMenuItem("Close", icon);
Jbutton b1=new Jbutton(“exit”,icon);
A popup menu:-
Another type of a menu is a popup menu. It is
sometimes called a context menu. It is usually shown,
when we right click on a component. The idea is to
provide only the commands, that are relevant to the
current context. Say we have an image. By right clicking
on the image, we get a window with commands to save,
rescale, move etc. the
image.
private JPopupMenu menu;
menu = new JPopupMenu();
JMenuItem menuItemBeep = new JMenuItem("Beep");
menuItemBeep.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
toolkit.beep();
}
});
menu.add(menuItemBeep);
JMenuItem menuItemClose = new JMenuItem("Close");
menuItemClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(menuItemClose);
frame.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (e.getButton() == e.BUTTON3) // 1 & 2
{
menu.show(e.getComponent(), e.getX(), e.getY());
}
}
});
Toolbar:-javax.swing.JToolBar;
Menus group commands that we can use in an
application.
Toolbars provide a quick access to the most frequently
used commands.
In Java Swing, the JToolbar class creates a toolbar in an
application.
ImageIcon icon = new
ImageIcon("exit.png");
ToolBar toolbar = new JToolBar();
JButton exit = new JButton(icon);
toolbar.add(exit);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
add(toolbar, BorderLayout.NORTH);
Swings in java

More Related Content

What's hot (20)

GUI components in Java
GUI components in JavaGUI components in Java
GUI components in Java
 
Java swing
Java swingJava swing
Java swing
 
Java swing
Java swingJava swing
Java swing
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
Java- GUI- Mazenet solution
Java- GUI- Mazenet solutionJava- GUI- Mazenet solution
Java- GUI- Mazenet solution
 
Awt and swing in java
Awt and swing in javaAwt and swing in java
Awt and swing in java
 
Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFC
 
Java awt tutorial javatpoint
Java awt tutorial   javatpointJava awt tutorial   javatpoint
Java awt tutorial javatpoint
 
tL19 awt
tL19 awttL19 awt
tL19 awt
 
Z blue introduction to gui (39023299)
Z blue   introduction to gui (39023299)Z blue   introduction to gui (39023299)
Z blue introduction to gui (39023299)
 
Jdbc
JdbcJdbc
Jdbc
 
Gui
GuiGui
Gui
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
 
28 awt
28 awt28 awt
28 awt
 
Swings
SwingsSwings
Swings
 
Java Swing
Java SwingJava Swing
Java Swing
 
04b swing tutorial
04b swing tutorial04b swing tutorial
04b swing tutorial
 
Swing and Graphical User Interface in Java
Swing and Graphical User Interface in JavaSwing and Graphical User Interface in Java
Swing and Graphical User Interface in Java
 
java2 swing
java2 swingjava2 swing
java2 swing
 

Similar to Swings in java

The java swing_tutorial
The java swing_tutorialThe java swing_tutorial
The java swing_tutorialsumitjoshi01
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892renuka gavli
 
java presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swingsjava presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on SwingsMohanYedatkar
 
Throughout the semester, we have been working on command line applic.pdf
Throughout the semester, we have been working on command line applic.pdfThroughout the semester, we have been working on command line applic.pdf
Throughout the semester, we have been working on command line applic.pdfbirajdar2
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingPayal Dungarwal
 
Java AWT and Java FX
Java AWT and Java FXJava AWT and Java FX
Java AWT and Java FXpratikkadam78
 
Unit-2 swing and mvc architecture
Unit-2 swing and mvc architectureUnit-2 swing and mvc architecture
Unit-2 swing and mvc architectureAmol Gaikwad
 
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
 
Java Swing Handson Session (1).ppt
Java Swing Handson Session (1).pptJava Swing Handson Session (1).ppt
Java Swing Handson Session (1).pptssuser076380
 

Similar to Swings in java (20)

The java swing_tutorial
The java swing_tutorialThe java swing_tutorial
The java swing_tutorial
 
The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
 
Chap1 1 1
Chap1 1 1Chap1 1 1
Chap1 1 1
 
Chap1 1.1
Chap1 1.1Chap1 1.1
Chap1 1.1
 
java presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swingsjava presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swings
 
Throughout the semester, we have been working on command line applic.pdf
Throughout the semester, we have been working on command line applic.pdfThroughout the semester, we have been working on command line applic.pdf
Throughout the semester, we have been working on command line applic.pdf
 
swings.pptx
swings.pptxswings.pptx
swings.pptx
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.Swing
 
Swing
SwingSwing
Swing
 
Swing
SwingSwing
Swing
 
Java AWT and Java FX
Java AWT and Java FXJava AWT and Java FX
Java AWT and Java FX
 
08graphics
08graphics08graphics
08graphics
 
11basic Swing
11basic Swing11basic Swing
11basic Swing
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Unit-2 swing and mvc architecture
Unit-2 swing and mvc architectureUnit-2 swing and mvc architecture
Unit-2 swing and mvc architecture
 
Ajp notes-chapter-02
Ajp notes-chapter-02Ajp notes-chapter-02
Ajp notes-chapter-02
 
Basic swing
Basic swingBasic swing
Basic swing
 
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
 
Java Swing Handson Session (1).ppt
Java Swing Handson Session (1).pptJava Swing Handson Session (1).ppt
Java Swing Handson Session (1).ppt
 

Recently uploaded

(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
(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
 
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
 
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
 
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
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
(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
 
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
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
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
 

Recently uploaded (20)

(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.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
 
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
 
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...
 
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
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
(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...
 
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...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
★ 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
 
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
 

Swings in java

  • 1. By :- Mrs. Jyoti Totla Assistant Professor(CSE) Mewar University
  • 2. Swing  Swing library is an official Java GUI toolkit released by Sun Microsystems.  Swing is probably the most advanced toolkit on this planet.  It has a rich set of widgets.  From basic widgets like Buttons, Labels, Scrollbars to advanced widgets like Trees and Tables.  Swing is written in 100% java.
  • 3.  Swing is a part of JFC, Java Foundation Classes. It is a collection of packages for creating full featured desktop applications. SWT library  There is also another GUI library for the Java programming language. It is called SWT. The Standard widget toolkit. The SWT library was initially developed by the IBM corporation.
  • 4.  Now it is an open source project, supported by IBM. The SWT is an example of a heavyweight toolkit. It lets the underlying OS to create GUI. SWT uses the java native interface to do the job.  The main advantages of the SWT are speed and native look and feel.  The SWT is on the other hand more error prone. It is less powerful then Swing. It is also quite Windows centric library.
  • 5. Basic window. import javax.swing.JFrame; public class Simple extends JFrame { public Simple() { setSize(300, 200); setTitle("Simple JFrame"); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { Simple simple = new Simple(); simple.setVisible(true); } }
  • 6.  import javax.swing.JFrame;  Here we import the JFrame widget. It is a top level container, which is used for placing other widgets.  setSize(300, 200);  setTitle("Simple");  This code will resize the window to be 300px wide and 200px tall. It will set the title of the  window to Simple.  setDefaultCloseOperation(EXIT_ON_CLOSE);  This method will close the window, if we click on the close button. By default nothing  happens.
  • 7. Set Image icon for Frame:-  ImageIcon icon= new ImageIcon("1.jpg");  f.setIconImage(icon.getImage());
  • 8. Keep the frame window in center: As per Screen Resolution.:- Package:-java.awt.Toolkit; java.awt.Dimension; Toolkit toolkit = getToolkit(); Dimension size = toolkit.getScreenSize(); setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2); }
  • 9. Jpanel:-javax.swing.JPanel; JPanel panel = new JPanel(); getContentPane().add(panel); We create a JPanel component. We add the JPanel to the JFrame. By default, the JPanel has a FlowLayout manager. The layout manager is used to place widgets onto the containers. If we call setLayout(null) we can position our components. absolutely. For this, we use the setBounds() method. panel.setLayout(null);
  • 10. JButtons:- package: javax.swing.JButton; JButton beep = new JButton("Beep"); beep.setBounds(150, 60, 80, 30); beep.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { toolkit.beep(); //System.exit(0);  }  });
  • 11. A tooltip  Tooltips are part of the internal application's help system. The Swing shows a small rectangular window, if we hover a mouse pointer over an object.  button.setToolTipText("A button component");
  • 12. MenuBar:-  A menubar is one of the most visible parts of the GUI application. It is a group of commands located in various menus. While in console applications you had to remember all those arcane commands, here we have most of the commands grouped into logical parts. There are accepted standards that further reduce the amount of time spending to learn a new application.  In Java Swing, to implement a menubar, we use three objects. A JMenuBar, a JMenu and  a JMenuItem.
  • 13. Packages used:- import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; file.setMnemonic(KeyEvent.VK_F); javax.swing.JCheckBoxMenuItem; ImageIcon:- ImageIcon icon = new ImageIcon("exit.png"); JMenuItem Close = new JMenuItem("Close", icon); Jbutton b1=new Jbutton(“exit”,icon);
  • 14. A popup menu:- Another type of a menu is a popup menu. It is sometimes called a context menu. It is usually shown, when we right click on a component. The idea is to provide only the commands, that are relevant to the current context. Say we have an image. By right clicking on the image, we get a window with commands to save, rescale, move etc. the image.
  • 15. private JPopupMenu menu; menu = new JPopupMenu(); JMenuItem menuItemBeep = new JMenuItem("Beep"); menuItemBeep.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { toolkit.beep(); } }); menu.add(menuItemBeep); JMenuItem menuItemClose = new JMenuItem("Close"); menuItemClose.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } });
  • 16. menu.add(menuItemClose); frame.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { if (e.getButton() == e.BUTTON3) // 1 & 2 { menu.show(e.getComponent(), e.getX(), e.getY()); } } });
  • 17. Toolbar:-javax.swing.JToolBar; Menus group commands that we can use in an application. Toolbars provide a quick access to the most frequently used commands. In Java Swing, the JToolbar class creates a toolbar in an application.
  • 18. ImageIcon icon = new ImageIcon("exit.png"); ToolBar toolbar = new JToolBar(); JButton exit = new JButton(icon); toolbar.add(exit); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); add(toolbar, BorderLayout.NORTH);