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

PPT
Java Swing
PPTX
PPTX
Java- GUI- Mazenet solution
PPT
java swing
PPT
Awt and swing in java
PPT
Swing and AWT in java
PPTX
Complete java swing
Java Swing
Java- GUI- Mazenet solution
java swing
Awt and swing in java
Swing and AWT in java
Complete java swing

What's hot (20)

PPTX
Chapter 1 swings
PPTX
Java swing
PPT
Java swings
PPT
Java Swing JFC
PDF
04b swing tutorial
PDF
JAVA GUI PART I
PPT
Graphical User Interface (GUI) - 1
PPT
Basic using of Swing in Java
PPTX
Java Swing
PDF
The AWT and Swing
PPT
Java swing
PDF
swingbasics
PPTX
Java swing
PPTX
Swings in java
PPTX
GUI Programming with Java
PPTX
GUI components in Java
PPTX
tL19 awt
PDF
Java GUI PART II
PPTX
GUI programming
Chapter 1 swings
Java swing
Java swings
Java Swing JFC
04b swing tutorial
JAVA GUI PART I
Graphical User Interface (GUI) - 1
Basic using of Swing in Java
Java Swing
The AWT and Swing
Java swing
swingbasics
Java swing
Swings in java
GUI Programming with Java
GUI components in Java
tL19 awt
Java GUI PART II
GUI programming
Ad

Viewers also liked (20)

PPT
PPTX
Chapter 11.4
PPTX
Power editor basics
PPTX
PPT
011 more swings_adv
PPT
PPT
java swing programming
PPTX
Tinn Capital 2010 piet van vugt
PPTX
2014 naples
PPTX
2014 villefranche
PPTX
Nobel Visie
PPTX
Cloudxp keynote 19 sept pvu
PPT
Oracle 11i OID AD Integration
PDF
Nilai un dan_us_2010_ips
PDF
AMD Putting Server Virtualization to Work
PPT
Cell :: Properties
PDF
IDC Tech Spotlight: From Silicon To Cloud
PPS
Key Compliances in Investing Abroad | Vinita Bahri-Mehra
PDF
Catalyst Eye Tracking: Bing vs Google
PPT
Cap Editing
Chapter 11.4
Power editor basics
011 more swings_adv
java swing programming
Tinn Capital 2010 piet van vugt
2014 naples
2014 villefranche
Nobel Visie
Cloudxp keynote 19 sept pvu
Oracle 11i OID AD Integration
Nilai un dan_us_2010_ips
AMD Putting Server Virtualization to Work
Cell :: Properties
IDC Tech Spotlight: From Silicon To Cloud
Key Compliances in Investing Abroad | Vinita Bahri-Mehra
Catalyst Eye Tracking: Bing vs Google
Cap Editing
Ad

Similar to java2 swing (20)

PPTX
it's about the swing programs in java language
PDF
Getting started with GUI programming in Java_1
PDF
11basic Swing
PDF
Basic swing
PDF
PLEASE HELP ME !!IT IS Due Tonight ;(!How can I make the add but.pdf
PPTX
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
PPTX
PPTX
swings.pptx
PDF
package buttongui; import static com.sun.deploy.config.JREInf.pdf
PPTX
01. introduction to swing
PPTX
A Simple Java GUI Application.pptx
PPTX
Awt, Swing, Layout managers
PDF
Swingpre 150616004959-lva1-app6892
PPTX
09 gui
PPTX
09 gui
PPTX
09 gui
PPTX
UNIT 2 SWIGS for java programing by .pptx
DOC
java swing notes in easy manner for UG students
it's about the swing programs in java language
Getting started with GUI programming in Java_1
11basic Swing
Basic swing
PLEASE HELP ME !!IT IS Due Tonight ;(!How can I make the add but.pdf
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
swings.pptx
package buttongui; import static com.sun.deploy.config.JREInf.pdf
01. introduction to swing
A Simple Java GUI Application.pptx
Awt, Swing, Layout managers
Swingpre 150616004959-lva1-app6892
09 gui
09 gui
09 gui
UNIT 2 SWIGS for java programing by .pptx
java swing notes in easy manner for UG students

Recently uploaded (20)

PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
cuic standard and advanced reporting.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Cloud computing and distributed systems.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Review of recent advances in non-invasive hemoglobin estimation
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
sap open course for s4hana steps from ECC to s4
MYSQL Presentation for SQL database connectivity
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
cuic standard and advanced reporting.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The AUB Centre for AI in Media Proposal.docx
Understanding_Digital_Forensics_Presentation.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Cloud computing and distributed systems.
Digital-Transformation-Roadmap-for-Companies.pptx
Big Data Technologies - Introduction.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Review of recent advances in non-invasive hemoglobin estimation

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