SlideShare a Scribd company logo
Java Swing Chris North cs3724:  HCI
AWT to Swing AWT:  Abstract Windowing Toolkit import java.awt.* Swing:  new with Java2 import javax.swing.* Extends AWT Tons o’ new improved components Standard dialog boxes, tooltips, … Look-and-feel, skins Event listeners API:  http://java.sun.com/j2se/1.3/docs/api/index.html
Swing Set Demo J2sdk/demo/jfc/SwingSet2 Many predefined    GUI components
GUI Component API Java:  GUI component = class Properties Methods Events JButton
Using a GUI Component Create it Instantiate object:  b = new JButton(“press me”); Configure it Properties:  b.text = “press me”;  [avoided in java] Methods:  b.setText(“press me”); Add it panel.add(b); Listen to it Events:  Listeners JButton
Anatomy of an Application GUI JPanel JButton JFrame JLabel GUI Internal structure JFrame JPanel JButton JLabel containers
Using a GUI Component 2 Create it Configure it Add children  (if container) Add to parent  (if not JFrame) Listen to it order important
Build from bottom up Create: Frame Panel Components Listeners Add:  (bottom up) listeners into components components into panel panel into frame JPanel JButton Listener JFrame JLabel
Code JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); JButton b = new JButton(“press me”); p.add(b);   // add button to panel f.setContentPane(p);  // add panel to frame f.show(); press me
Application Code import javax.swing.*; class hello { public static void main(String[] args){ JFrame f = new JFrame(“title”); JPanel p = new JPanel(); JButton b = new JButton(“press me”); p.add(b); // add button to panel f.setContentPane(p);  // add panel to frame f.show(); } } press me
Layout Managers Automatically control placement of components in a panel Why?
Layout Manager Heuristics Left to right, Top to bottom c n s e w FlowLayout GridLayout BorderLayout none,  programmer  sets x,y,w,h null One at a time CardLayout GridBagLayout JButton
Combinations JButton JButton JTextArea
Combinations n JPanel:  BorderLayout c JFrame JPanel:  FlowLayout JButton JButton JTextArea
Code:  null layout JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); JButton b = new JButton(“press me”); b.setBounds(new Rectangle(10,10, 100,50)); p.setLayout(null); // x,y layout p.add(b); f.setContentPane(p); press me
Code:  FlowLayout JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); FlowLayout L = new FlowLayout( ); JButton b1 = new JButton(“press me”); JButton b2 = new JButton(“then me”); p.setLayout(L); p.add(b1); p.add(b2); f.setContentPane(p); Set layout mgr before adding components press me then me
Applets JApplet is like a JFrame Already has a panel Access panel with JApplet.getContentPane( ) import javax.swing.*; class hello  extends JApplet  { public void  init (){ JButton b = new JButton(“press me”); getContentPane().add(b); } } JApplet contentPane JButton
Applet Methods Called by browser: init( )  - initialization start( )  - resume processing (e.g. animations) stop( ) - pause destroy( ) - cleanup paint( ) - redraw stuff (‘expose’ event)
Application + Applet import javax.swing.*; class helloApp { public static void main(String[] args){ // create Frame and put my mainPanel in it JFrame f = new JFrame(“title”); mainPanel p = new mainPanel(); f.setContentPane(p); f.show(); } } class helloApplet extends JApplet { public void init(){ // put my mainPanel in the Applet mainPanel p = new mainPanel(); getContentPane().add(p); } } // my main GUI is in here: class mainPanel extends JPanel { mainPanel(){ setLayout(new FlowLayout()); JButton b = new JButton(“press me”); add(b); } } JApplet contentPane JPanel JFrame JButton or Browser Command line
Applet Security No read/write on client machine Can’t execute programs on client machine Communicate only with server “Java applet window” Warning
In JBuilder

More Related Content

What's hot (20)

PPTX
Chapter 1 swings
Jafar Nesargi
 
PPTX
Java swing
ssuser3a47cb
 
PPT
Java swings
Alisha Korpal
 
PPT
Java Swing JFC
Sunil OS
 
PDF
04b swing tutorial
Prakash Sweet
 
PDF
JAVA GUI PART I
OXUS 20
 
PPT
Graphical User Interface (GUI) - 1
PRN USM
 
PPT
Basic using of Swing in Java
suraj pandey
 
PPTX
Java Swing
Arkadeep Dey
 
PDF
The AWT and Swing
adil raja
 
PPT
Java swing
Nataraj Dg
 
PDF
swingbasics
Arjun Shanka
 
PPTX
Java swing
Apurbo Datta
 
PPTX
Swings in java
Jyoti Totla
 
PPTX
GUI Programming with Java
Jussi Pohjolainen
 
PPTX
GUI components in Java
kirupasuchi1996
 
PPTX
tL19 awt
teach4uin
 
PDF
Java GUI PART II
OXUS 20
 
PPTX
GUI programming
Vineeta Garg
 
Chapter 1 swings
Jafar Nesargi
 
Java swing
ssuser3a47cb
 
Java swings
Alisha Korpal
 
Java Swing JFC
Sunil OS
 
04b swing tutorial
Prakash Sweet
 
JAVA GUI PART I
OXUS 20
 
Graphical User Interface (GUI) - 1
PRN USM
 
Basic using of Swing in Java
suraj pandey
 
Java Swing
Arkadeep Dey
 
The AWT and Swing
adil raja
 
Java swing
Nataraj Dg
 
swingbasics
Arjun Shanka
 
Java swing
Apurbo Datta
 
Swings in java
Jyoti Totla
 
GUI Programming with Java
Jussi Pohjolainen
 
GUI components in Java
kirupasuchi1996
 
tL19 awt
teach4uin
 
Java GUI PART II
OXUS 20
 
GUI programming
Vineeta Garg
 

Viewers also liked (20)

PPT
Swing
Bharat17485
 
PPTX
Chapter 11.4
sotlsoc
 
PPTX
Power editor basics
Emerita Salvador
 
PPTX
Jtextarea
Andrés Jaimes Crespo
 
PPT
011 more swings_adv
Chaimaa Kabb
 
PPT
JDBC
Ankit Desai
 
PPT
java swing programming
Ankit Desai
 
PPTX
Tinn Capital 2010 piet van vugt
Piet van Vugt
 
PPTX
2014 naples
c.titus.brown
 
PPTX
2014 villefranche
c.titus.brown
 
PPTX
Nobel Visie
Piet van Vugt
 
PPTX
Cloudxp keynote 19 sept pvu
Piet van Vugt
 
PPT
Oracle 11i OID AD Integration
Mahesh Vallampati
 
PDF
Nilai un dan_us_2010_ips
@rtNya
 
PDF
AMD Putting Server Virtualization to Work
James Price
 
PPT
Cell :: Properties
rejita
 
PDF
IDC Tech Spotlight: From Silicon To Cloud
James Price
 
PPS
Key Compliances in Investing Abroad | Vinita Bahri-Mehra
Kegler Brown Hill + Ritter
 
PDF
Catalyst Eye Tracking: Bing vs Google
Jennifer Hsieh
 
PPT
Cap Editing
guestb3214a
 
Chapter 11.4
sotlsoc
 
Power editor basics
Emerita Salvador
 
011 more swings_adv
Chaimaa Kabb
 
java swing programming
Ankit Desai
 
Tinn Capital 2010 piet van vugt
Piet van Vugt
 
2014 naples
c.titus.brown
 
2014 villefranche
c.titus.brown
 
Nobel Visie
Piet van Vugt
 
Cloudxp keynote 19 sept pvu
Piet van Vugt
 
Oracle 11i OID AD Integration
Mahesh Vallampati
 
Nilai un dan_us_2010_ips
@rtNya
 
AMD Putting Server Virtualization to Work
James Price
 
Cell :: Properties
rejita
 
IDC Tech Spotlight: From Silicon To Cloud
James Price
 
Key Compliances in Investing Abroad | Vinita Bahri-Mehra
Kegler Brown Hill + Ritter
 
Catalyst Eye Tracking: Bing vs Google
Jennifer Hsieh
 
Cap Editing
guestb3214a
 
Ad

Similar to java2 swing (20)

PPT
13457272.ppt
aptechaligarh
 
PPT
GUI Programming In Java
yht4ever
 
PDF
11basic Swing
Adil Jafri
 
PDF
Basic swing
bharathi120789
 
PDF
Z blue introduction to gui (39023299)
Narayana Swamy
 
PDF
Swingpre 150616004959-lva1-app6892
renuka gavli
 
PDF
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
MarlouFelixIIICunana
 
PPTX
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
Salini P
 
PDF
Ingles 2do parcial
Harry Ostaiza
 
PPTX
Abstract Window Toolkit_Event Handling_python
jasminebeulahg
 
PPT
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
havalneha2121
 
PPT
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
SulbhaBhivsane
 
PPT
1.Abstract windowing toolkit.ppt of AJP sub
YugandharaNalavade
 
PPT
Unit 1- awt(Abstract Window Toolkit) .ppt
Deepgaichor1
 
PPT
28 awt
Prachi Vijh
 
PPT
introduction to JAVA awt programmin .ppt
bgvthm
 
PPTX
UNIT 2 SWIGS for java programing by .pptx
st5617067
 
PPT
14a-gui.ppt
DrDGayathriDevi
 
PPTX
Swing component point are mentioned in PPT which helpgul for creating Java GU...
sonalipatil225940
 
PPT
25 awt
degestive
 
13457272.ppt
aptechaligarh
 
GUI Programming In Java
yht4ever
 
11basic Swing
Adil Jafri
 
Basic swing
bharathi120789
 
Z blue introduction to gui (39023299)
Narayana Swamy
 
Swingpre 150616004959-lva1-app6892
renuka gavli
 
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
MarlouFelixIIICunana
 
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
Salini P
 
Ingles 2do parcial
Harry Ostaiza
 
Abstract Window Toolkit_Event Handling_python
jasminebeulahg
 
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
havalneha2121
 
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
SulbhaBhivsane
 
1.Abstract windowing toolkit.ppt of AJP sub
YugandharaNalavade
 
Unit 1- awt(Abstract Window Toolkit) .ppt
Deepgaichor1
 
28 awt
Prachi Vijh
 
introduction to JAVA awt programmin .ppt
bgvthm
 
UNIT 2 SWIGS for java programing by .pptx
st5617067
 
14a-gui.ppt
DrDGayathriDevi
 
Swing component point are mentioned in PPT which helpgul for creating Java GU...
sonalipatil225940
 
25 awt
degestive
 
Ad

Recently uploaded (20)

PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Python basic programing language for automation
DanialHabibi2
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Python basic programing language for automation
DanialHabibi2
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
July Patch Tuesday
Ivanti
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 

java2 swing

  • 1. Java Swing Chris North cs3724: HCI
  • 2. AWT to Swing AWT: Abstract Windowing Toolkit import java.awt.* Swing: new with Java2 import javax.swing.* Extends AWT Tons o’ new improved components Standard dialog boxes, tooltips, … Look-and-feel, skins Event listeners API: http://java.sun.com/j2se/1.3/docs/api/index.html
  • 3. Swing Set Demo J2sdk/demo/jfc/SwingSet2 Many predefined GUI components
  • 4. GUI Component API Java: GUI component = class Properties Methods Events JButton
  • 5. Using a GUI Component Create it Instantiate object: b = new JButton(“press me”); Configure it Properties: b.text = “press me”; [avoided in java] Methods: b.setText(“press me”); Add it panel.add(b); Listen to it Events: Listeners JButton
  • 6. Anatomy of an Application GUI JPanel JButton JFrame JLabel GUI Internal structure JFrame JPanel JButton JLabel containers
  • 7. Using a GUI Component 2 Create it Configure it Add children (if container) Add to parent (if not JFrame) Listen to it order important
  • 8. Build from bottom up Create: Frame Panel Components Listeners Add: (bottom up) listeners into components components into panel panel into frame JPanel JButton Listener JFrame JLabel
  • 9. Code JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); JButton b = new JButton(“press me”); p.add(b); // add button to panel f.setContentPane(p); // add panel to frame f.show(); press me
  • 10. Application Code import javax.swing.*; class hello { public static void main(String[] args){ JFrame f = new JFrame(“title”); JPanel p = new JPanel(); JButton b = new JButton(“press me”); p.add(b); // add button to panel f.setContentPane(p); // add panel to frame f.show(); } } press me
  • 11. Layout Managers Automatically control placement of components in a panel Why?
  • 12. Layout Manager Heuristics Left to right, Top to bottom c n s e w FlowLayout GridLayout BorderLayout none, programmer sets x,y,w,h null One at a time CardLayout GridBagLayout JButton
  • 14. Combinations n JPanel: BorderLayout c JFrame JPanel: FlowLayout JButton JButton JTextArea
  • 15. Code: null layout JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); JButton b = new JButton(“press me”); b.setBounds(new Rectangle(10,10, 100,50)); p.setLayout(null); // x,y layout p.add(b); f.setContentPane(p); press me
  • 16. Code: FlowLayout JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); FlowLayout L = new FlowLayout( ); JButton b1 = new JButton(“press me”); JButton b2 = new JButton(“then me”); p.setLayout(L); p.add(b1); p.add(b2); f.setContentPane(p); Set layout mgr before adding components press me then me
  • 17. Applets JApplet is like a JFrame Already has a panel Access panel with JApplet.getContentPane( ) import javax.swing.*; class hello extends JApplet { public void init (){ JButton b = new JButton(“press me”); getContentPane().add(b); } } JApplet contentPane JButton
  • 18. Applet Methods Called by browser: init( ) - initialization start( ) - resume processing (e.g. animations) stop( ) - pause destroy( ) - cleanup paint( ) - redraw stuff (‘expose’ event)
  • 19. Application + Applet import javax.swing.*; class helloApp { public static void main(String[] args){ // create Frame and put my mainPanel in it JFrame f = new JFrame(“title”); mainPanel p = new mainPanel(); f.setContentPane(p); f.show(); } } class helloApplet extends JApplet { public void init(){ // put my mainPanel in the Applet mainPanel p = new mainPanel(); getContentPane().add(p); } } // my main GUI is in here: class mainPanel extends JPanel { mainPanel(){ setLayout(new FlowLayout()); JButton b = new JButton(“press me”); add(b); } } JApplet contentPane JPanel JFrame JButton or Browser Command line
  • 20. Applet Security No read/write on client machine Can’t execute programs on client machine Communicate only with server “Java applet window” Warning