SlideShare a Scribd company logo
1 of 46
GUI – Programming with Java
JFRAME
Creating Windows Windows javax.swing.JFrame – Empty window By default, JFrame is not visible setVisible(true)! Java Look And Feel (Metal) by default
Creating JFrame import javax.swing.JFrame; class Testing {     public static void main(String [] args) {         JFrame mywin = new JFrame();         mywin.setVisible(true);     } }
Inheritance import javax.swing.JFrame; class MyJFrame extends JFrame {     public MyJFrame() {         setTitle("My Window!");     } } class Testing {     public static void main(String [] args) {         MyJFrame mywin = new MyJFrame();         mywin.setVisible(true);     } }
GUI
AWT vs. Swing AWT = Abstract Window Toolkit java.awt.*; Old class library for Java Uses native GUI - components Swing javax.swing.*; From Java 1.2 -> Extends AWT GUI – components 100% Java Pluggable look and feel
Layouts UI is build on top of layouts Layout determinates where UI-elements are layed. You can add a layout to Jframe Layouts FlowLayout GridLayout BorderLayout GridBagLAyout CardLayout
Layout and Components import javax.swing.*; import java.awt.*; class MyJFrame extends JFrame {     public MyJFrame() {         setTitle("My Window!");         JButton clickMe = new JButton("Click me!");         FlowLayout layout = new FlowLayout();         setLayout(layout);         add(clickMe);     } } class Testing {     public static void main(String [] args) {         MyJFrame mywin = new MyJFrame();         mywin.setVisible(true);     } }
FlowLayout
GridLayout
BorderLayout Five cells north south east west center
Other layouts BoxLayout - Components in row or column CardLayout ,[object Object],hold other components GridBagLayout - Lot of possibilities, hard to use
JPanel It's possible to combine layouts using JPanel JPanel can have it's own layout. JPanel may hold other components JFrame can hold JPanels, that hold components..
Combining Layouts JPanel left = new JPanel(); left.setLayout(new GridLayout(2,1)); JPanel right = new JPanel(); right.setLayout(new GridLayout(3,1)); left right setLayout(new Gridlayout(1,2)); add(left); add(right); MyJFrame
Combining Layouts public MyJFrame(){   setLayout(new GridLayout(1,2)); JPanel left = new JPanel(); left.setLayout(new GridLayout(2,1)); left.add(new JButton("1")); left.add(new JButton("2")); JPanel right = new JPanel(); right.setLayout(new GridLayout(3,1)); right.add(new JButton("3")); right.add(new JButton("4"));   right.add(new JButton("5")); add(left);   add(right); }
Components
Components UI – components can be added to window (JFrame) using the add() – method All components derive from JComponent UI – components? JButton JLabel JMenuItem JTable JTextArea JTextField...
JComponent Some component's common methods setVisible(boolean) setFont(Font) setEnabled(boolean) ... See JComponent API http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html
Components JButton JButton mybutton = new JButton("Click!"); JLabel JLabel mylabel = new JLabel("Some text"); JTextField JTextField myfield = new JTextField(); String text = myfield.getText(); JTextArea...
Event handling
Delegation Event Handling Delegation Event  Model: Simple and easy to learn Support a clean separation between application and GUI code Facilitate the creation of robust event handling code which is less error-prone (strong compile-time checking) Flexible enough to enable varied application models for event flow and propagation For visual tool builders, enable run-time discovery of both events that a component generates as well as the events it may observe Support backward binary compatibility with the old model
Separation of GUI and BL Source Listener Registration
Concepts: Event Source Event source is usually some component that can raise events Examples of event source JButton (button is clicked) JMenuItem JTextField
Listener Any class that can handle the events => Any class that implements some interface
Separation of GUI and BL Source Listener Registration
Recap on Polymorphism interface AbleToMove {     public void start();     public void stop(); } class Car implements AbleToMove { 	public void start() { 		// do something    }    public void stop() {     // do something    } }
Recap on Polymorphism class Test { 	public static void main(String [] args) { 	    Airplane a = new Airplane(); 		   someMethod(a);    }    public static void someMethod(AbleToMove x) { 			x.start();    } } You can pass whatever object you desire as  long as the object implements AbleToMove interface!
Example JButton is a event source. JButton source = new JButton(); When the button is clicked we want that something happens We need a class that listens to the button. We register the source and the listener with each other!
Separation of GUI and BL JButton Listener Registration
Registration JButton holds a method addActionListener public void addActionListener(ActionListener l) So you can call it like JButton source = new JButton("Click!"); source.addActionListener(...); Parameter ActionListener? What is it? It's an interface! This means that you can pass whatever object as long as the object implements the interface!
ActionListener So the listener can be what ever object as long as it implements the ActionListener. ActionListener: interface ActionListener { public void actionPerformed(ActionEvent e); }
Listener Some class that implements the ActionListener class MyListener implements ActionListener {     public void actionPerformed(ActionEvent e) {        // do something    } }
Separation of GUI and BL JButton MyListener addActionListener ActionListener
Example Usage // Create the source JButton button = new JButton("click me"); // Create the listener MyListener listener = new MyListener(); // Registration button.addActionListener(listener);
Registration Different sources have different methods and interfaces for the registration. Registration: addXXXListener Examples addMouseMotionListener(...) addMouseListener(...) addKeyListener(...)
Example: Listening to Mouse in Window // Source JFrame a = new JFrame(); // Listener SomeClass listener = new SomeClass(); // Registration a.addMouseMotionListener(listener);
BL and GUI in the same class In small programs it is usual that the GUI - and the application code is implemented in the same class. No the listener is the same class: class MyJFrame extends JFrame implements ActionListener And registration: source.addActionListener(this);
Menus
MenuBar JFrame can contain MenuBar Needed classes JMenuBar JMenu JMenuItem Event handling the same as in JButton (addActionListener)
Creating MenuBar JMenuBar menubar = new JMenuBar(); JMenu edit = new JMenu("Edit"); JMenuItem pref = new JMenu("Preferen.. edit.add(pref); menubar.add(edit); setJMenuBar(menubar);
Dialogs
About Dialogs JFrame is used for windows, JDialog for dialogs. You can inherit the JDialog. Every dialog needs to know who the host window is dialog belongs to the window
Standard Dialogs Really easy way to create dialogs is use standard dialogs JOptionPane JFileChooser JColorChooser ...
JOptionPane With one line of code, you can pop up a dialog JOptionPane.showMessageDialog(hostframe, "Hello!"); Also available showConfirmDialog yes/no/cancel showInputDialog prompt user
JFileChooser JFileChooser chooser = new JFileChooser();  int returnVal = chooser.showOpenDialog(parent);  if(returnVal == JFileChooser.APPROVE_OPTION)     String file = chooser.getSelectedFile().getName();

More Related Content

What's hot (20)

Awt
AwtAwt
Awt
 
The AWT and Swing
The AWT and SwingThe AWT and Swing
The AWT and Swing
 
Awt and swing in java
Awt and swing in javaAwt and swing in java
Awt and swing in java
 
GUI programming
GUI programmingGUI programming
GUI programming
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
tL19 awt
tL19 awttL19 awt
tL19 awt
 
Swings
SwingsSwings
Swings
 
Complete java swing
Complete java swingComplete java swing
Complete java swing
 
java swing
java swingjava swing
java swing
 
Awt controls ppt
Awt controls pptAwt controls ppt
Awt controls ppt
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Graphical User Interface (Gui)
Graphical User Interface (Gui)Graphical User Interface (Gui)
Graphical User Interface (Gui)
 
Java swing
Java swingJava swing
Java swing
 
Chapter 1 swings
Chapter 1 swingsChapter 1 swings
Chapter 1 swings
 
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
 
Java swing
Java swingJava swing
Java swing
 
Graphical User Interface in JAVA
Graphical User Interface in JAVAGraphical User Interface in JAVA
Graphical User Interface in JAVA
 
Java awt tutorial javatpoint
Java awt tutorial   javatpointJava awt tutorial   javatpoint
Java awt tutorial javatpoint
 
28 awt
28 awt28 awt
28 awt
 
java2 swing
java2 swingjava2 swing
java2 swing
 

Viewers also liked

Mental models
Mental modelsMental models
Mental modelsaukee
 
Modul oop with java application mauludin
Modul oop with java application   mauludinModul oop with java application   mauludin
Modul oop with java application mauludinMauludin Ahmad
 
The Game Of Life - Java‘s Siblings and Heirs are populating the Ecosystem
The Game Of Life - Java‘s Siblings and Heirs are populating  the EcosystemThe Game Of Life - Java‘s Siblings and Heirs are populating  the Ecosystem
The Game Of Life - Java‘s Siblings and Heirs are populating the Ecosystemjexp
 
KC Java Android Talk (March 2011)
KC Java Android Talk (March 2011)KC Java Android Talk (March 2011)
KC Java Android Talk (March 2011)osake
 
First Steps in Android
First Steps in AndroidFirst Steps in Android
First Steps in AndroidRich Helton
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android DevelopmentProf. Erwin Globio
 
Google I/O 2013 報告会 Android Studio と Gradle
Google I/O 2013 報告会 Android Studio と GradleGoogle I/O 2013 報告会 Android Studio と Gradle
Google I/O 2013 報告会 Android Studio と GradleKeishin Yokomaku
 
Games and Java ME - Have fun and earn some money
Games and Java ME - Have fun and earn some moneyGames and Java ME - Have fun and earn some money
Games and Java ME - Have fun and earn some moneyMarcelo Quinta
 
Practical OOP In Java
Practical OOP In JavaPractical OOP In Java
Practical OOP In Javawiradikusuma
 
Android Development: The Basics
Android Development: The BasicsAndroid Development: The Basics
Android Development: The BasicsMike Desjardins
 
Contingency theory of management
Contingency theory of managementContingency theory of management
Contingency theory of managementARUN NAIK
 
Android for Java Developers
Android for Java DevelopersAndroid for Java Developers
Android for Java DevelopersMarko Gargenta
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 

Viewers also liked (20)

Gu iintro(java)
Gu iintro(java)Gu iintro(java)
Gu iintro(java)
 
Mental models
Mental modelsMental models
Mental models
 
Modul oop with java application mauludin
Modul oop with java application   mauludinModul oop with java application   mauludin
Modul oop with java application mauludin
 
The Game Of Life - Java‘s Siblings and Heirs are populating the Ecosystem
The Game Of Life - Java‘s Siblings and Heirs are populating  the EcosystemThe Game Of Life - Java‘s Siblings and Heirs are populating  the Ecosystem
The Game Of Life - Java‘s Siblings and Heirs are populating the Ecosystem
 
KC Java Android Talk (March 2011)
KC Java Android Talk (March 2011)KC Java Android Talk (March 2011)
KC Java Android Talk (March 2011)
 
First Steps in Android
First Steps in AndroidFirst Steps in Android
First Steps in Android
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
 
Google I/O 2013 報告会 Android Studio と Gradle
Google I/O 2013 報告会 Android Studio と GradleGoogle I/O 2013 報告会 Android Studio と Gradle
Google I/O 2013 報告会 Android Studio と Gradle
 
12 gui concepts 1
12 gui concepts 112 gui concepts 1
12 gui concepts 1
 
OOP in Java
OOP in JavaOOP in Java
OOP in Java
 
Games and Java ME - Have fun and earn some money
Games and Java ME - Have fun and earn some moneyGames and Java ME - Have fun and earn some money
Games and Java ME - Have fun and earn some money
 
Practical OOP In Java
Practical OOP In JavaPractical OOP In Java
Practical OOP In Java
 
java swing
java swingjava swing
java swing
 
Gui programming (awt)
Gui programming (awt)Gui programming (awt)
Gui programming (awt)
 
Android Development: The Basics
Android Development: The BasicsAndroid Development: The Basics
Android Development: The Basics
 
Contingency theory of management
Contingency theory of managementContingency theory of management
Contingency theory of management
 
Android ppt
Android pptAndroid ppt
Android ppt
 
Android for Java Developers
Android for Java DevelopersAndroid for Java Developers
Android for Java Developers
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 

Similar to Create Windows and GUI with Java Swing Framework

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
 
I am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdfI am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdffashionfolionr
 
Programming in java_-_17_-_swing
Programming in java_-_17_-_swingProgramming in java_-_17_-_swing
Programming in java_-_17_-_swingjosodo
 
Othello.javapackage othello;import core.Game; import userInter.pdf
Othello.javapackage othello;import core.Game; import userInter.pdfOthello.javapackage othello;import core.Game; import userInter.pdf
Othello.javapackage othello;import core.Game; import userInter.pdfarccreation001
 
Hi my question pretains to Java programing, What I am creating is a .pdf
Hi my question pretains to Java programing, What I am creating is a .pdfHi my question pretains to Java programing, What I am creating is a .pdf
Hi my question pretains to Java programing, What I am creating is a .pdfeyeonsecuritysystems
 
Groovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonGroovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonEric Wendelin
 
Groovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonGroovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonMatthew McCullough
 
This is Java, What I am creating is a multi lab pacman type game.T.pdf
This is Java, What I am creating is a multi lab pacman type game.T.pdfThis is Java, What I am creating is a multi lab pacman type game.T.pdf
This is Java, What I am creating is a multi lab pacman type game.T.pdffashionscollect
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managersswapnac12
 
What do you mean it needs to be Java based? How jython saved the day.
What do you mean it needs to be Java based? How jython saved the day.What do you mean it needs to be Java based? How jython saved the day.
What do you mean it needs to be Java based? How jython saved the day.Mark Rees
 

Similar to Create Windows and GUI with Java Swing Framework (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)
 
swings.pptx
swings.pptxswings.pptx
swings.pptx
 
I am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdfI am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdf
 
Programming in java_-_17_-_swing
Programming in java_-_17_-_swingProgramming in java_-_17_-_swing
Programming in java_-_17_-_swing
 
Othello.javapackage othello;import core.Game; import userInter.pdf
Othello.javapackage othello;import core.Game; import userInter.pdfOthello.javapackage othello;import core.Game; import userInter.pdf
Othello.javapackage othello;import core.Game; import userInter.pdf
 
Hi my question pretains to Java programing, What I am creating is a .pdf
Hi my question pretains to Java programing, What I am creating is a .pdfHi my question pretains to Java programing, What I am creating is a .pdf
Hi my question pretains to Java programing, What I am creating is a .pdf
 
Groovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonGroovy-er desktop applications with Griffon
Groovy-er desktop applications with Griffon
 
Groovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonGroovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With Griffon
 
This is Java, What I am creating is a multi lab pacman type game.T.pdf
This is Java, What I am creating is a multi lab pacman type game.T.pdfThis is Java, What I am creating is a multi lab pacman type game.T.pdf
This is Java, What I am creating is a multi lab pacman type game.T.pdf
 
SWING.pptx
SWING.pptxSWING.pptx
SWING.pptx
 
SwingApplet.pptx
SwingApplet.pptxSwingApplet.pptx
SwingApplet.pptx
 
Swing_Introduction.ppt
Swing_Introduction.pptSwing_Introduction.ppt
Swing_Introduction.ppt
 
Griffon @ Svwjug
Griffon @ SvwjugGriffon @ Svwjug
Griffon @ Svwjug
 
Java awt
Java awtJava awt
Java awt
 
Tuto jtatoo
Tuto jtatooTuto jtatoo
Tuto jtatoo
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
 
What do you mean it needs to be Java based? How jython saved the day.
What do you mean it needs to be Java based? How jython saved the day.What do you mean it needs to be Java based? How jython saved the day.
What do you mean it needs to be Java based? How jython saved the day.
 
CORE JAVA-2
CORE JAVA-2CORE JAVA-2
CORE JAVA-2
 
Swing
SwingSwing
Swing
 
Swing
SwingSwing
Swing
 

More from Jussi Pohjolainen

libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferencesJussi Pohjolainen
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationJussi Pohjolainen
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDXJussi Pohjolainen
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript DevelopmentJussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame AnimationJussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame AnimationJussi Pohjolainen
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDXJussi Pohjolainen
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDXJussi Pohjolainen
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesJussi Pohjolainen
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platformJussi Pohjolainen
 

More from Jussi Pohjolainen (20)

Moved to Speakerdeck
Moved to SpeakerdeckMoved to Speakerdeck
Moved to Speakerdeck
 
Java Web Services
Java Web ServicesJava Web Services
Java Web Services
 
Box2D and libGDX
Box2D and libGDXBox2D and libGDX
Box2D and libGDX
 
libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and Preferences
 
libGDX: Tiled Maps
libGDX: Tiled MapslibGDX: Tiled Maps
libGDX: Tiled Maps
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame Animation
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDX
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript Development
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
libGDX: Scene2D
libGDX: Scene2DlibGDX: Scene2D
libGDX: Scene2D
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
 
libGDX: User Input
libGDX: User InputlibGDX: User Input
libGDX: User Input
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDX
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDX
 
Android Threading
Android ThreadingAndroid Threading
Android Threading
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platform
 
Intro to Asha UI
Intro to Asha UIIntro to Asha UI
Intro to Asha UI
 

Recently uploaded

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Recently uploaded (20)

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

Create Windows and GUI with Java Swing Framework

  • 3. Creating Windows Windows javax.swing.JFrame – Empty window By default, JFrame is not visible setVisible(true)! Java Look And Feel (Metal) by default
  • 4. Creating JFrame import javax.swing.JFrame; class Testing { public static void main(String [] args) { JFrame mywin = new JFrame(); mywin.setVisible(true); } }
  • 5. Inheritance import javax.swing.JFrame; class MyJFrame extends JFrame { public MyJFrame() { setTitle("My Window!"); } } class Testing { public static void main(String [] args) { MyJFrame mywin = new MyJFrame(); mywin.setVisible(true); } }
  • 6. GUI
  • 7. AWT vs. Swing AWT = Abstract Window Toolkit java.awt.*; Old class library for Java Uses native GUI - components Swing javax.swing.*; From Java 1.2 -> Extends AWT GUI – components 100% Java Pluggable look and feel
  • 8. Layouts UI is build on top of layouts Layout determinates where UI-elements are layed. You can add a layout to Jframe Layouts FlowLayout GridLayout BorderLayout GridBagLAyout CardLayout
  • 9. Layout and Components import javax.swing.*; import java.awt.*; class MyJFrame extends JFrame { public MyJFrame() { setTitle("My Window!"); JButton clickMe = new JButton("Click me!"); FlowLayout layout = new FlowLayout(); setLayout(layout); add(clickMe); } } class Testing { public static void main(String [] args) { MyJFrame mywin = new MyJFrame(); mywin.setVisible(true); } }
  • 12. BorderLayout Five cells north south east west center
  • 13.
  • 14. JPanel It's possible to combine layouts using JPanel JPanel can have it's own layout. JPanel may hold other components JFrame can hold JPanels, that hold components..
  • 15. Combining Layouts JPanel left = new JPanel(); left.setLayout(new GridLayout(2,1)); JPanel right = new JPanel(); right.setLayout(new GridLayout(3,1)); left right setLayout(new Gridlayout(1,2)); add(left); add(right); MyJFrame
  • 16. Combining Layouts public MyJFrame(){ setLayout(new GridLayout(1,2)); JPanel left = new JPanel(); left.setLayout(new GridLayout(2,1)); left.add(new JButton("1")); left.add(new JButton("2")); JPanel right = new JPanel(); right.setLayout(new GridLayout(3,1)); right.add(new JButton("3")); right.add(new JButton("4")); right.add(new JButton("5")); add(left); add(right); }
  • 18. Components UI – components can be added to window (JFrame) using the add() – method All components derive from JComponent UI – components? JButton JLabel JMenuItem JTable JTextArea JTextField...
  • 19. JComponent Some component's common methods setVisible(boolean) setFont(Font) setEnabled(boolean) ... See JComponent API http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html
  • 20. Components JButton JButton mybutton = new JButton("Click!"); JLabel JLabel mylabel = new JLabel("Some text"); JTextField JTextField myfield = new JTextField(); String text = myfield.getText(); JTextArea...
  • 22. Delegation Event Handling Delegation Event Model: Simple and easy to learn Support a clean separation between application and GUI code Facilitate the creation of robust event handling code which is less error-prone (strong compile-time checking) Flexible enough to enable varied application models for event flow and propagation For visual tool builders, enable run-time discovery of both events that a component generates as well as the events it may observe Support backward binary compatibility with the old model
  • 23. Separation of GUI and BL Source Listener Registration
  • 24. Concepts: Event Source Event source is usually some component that can raise events Examples of event source JButton (button is clicked) JMenuItem JTextField
  • 25. Listener Any class that can handle the events => Any class that implements some interface
  • 26. Separation of GUI and BL Source Listener Registration
  • 27. Recap on Polymorphism interface AbleToMove { public void start(); public void stop(); } class Car implements AbleToMove { public void start() { // do something } public void stop() { // do something } }
  • 28. Recap on Polymorphism class Test { public static void main(String [] args) { Airplane a = new Airplane(); someMethod(a); } public static void someMethod(AbleToMove x) { x.start(); } } You can pass whatever object you desire as long as the object implements AbleToMove interface!
  • 29. Example JButton is a event source. JButton source = new JButton(); When the button is clicked we want that something happens We need a class that listens to the button. We register the source and the listener with each other!
  • 30. Separation of GUI and BL JButton Listener Registration
  • 31. Registration JButton holds a method addActionListener public void addActionListener(ActionListener l) So you can call it like JButton source = new JButton("Click!"); source.addActionListener(...); Parameter ActionListener? What is it? It's an interface! This means that you can pass whatever object as long as the object implements the interface!
  • 32. ActionListener So the listener can be what ever object as long as it implements the ActionListener. ActionListener: interface ActionListener { public void actionPerformed(ActionEvent e); }
  • 33. Listener Some class that implements the ActionListener class MyListener implements ActionListener { public void actionPerformed(ActionEvent e) { // do something } }
  • 34. Separation of GUI and BL JButton MyListener addActionListener ActionListener
  • 35. Example Usage // Create the source JButton button = new JButton("click me"); // Create the listener MyListener listener = new MyListener(); // Registration button.addActionListener(listener);
  • 36. Registration Different sources have different methods and interfaces for the registration. Registration: addXXXListener Examples addMouseMotionListener(...) addMouseListener(...) addKeyListener(...)
  • 37. Example: Listening to Mouse in Window // Source JFrame a = new JFrame(); // Listener SomeClass listener = new SomeClass(); // Registration a.addMouseMotionListener(listener);
  • 38. BL and GUI in the same class In small programs it is usual that the GUI - and the application code is implemented in the same class. No the listener is the same class: class MyJFrame extends JFrame implements ActionListener And registration: source.addActionListener(this);
  • 39. Menus
  • 40. MenuBar JFrame can contain MenuBar Needed classes JMenuBar JMenu JMenuItem Event handling the same as in JButton (addActionListener)
  • 41. Creating MenuBar JMenuBar menubar = new JMenuBar(); JMenu edit = new JMenu("Edit"); JMenuItem pref = new JMenu("Preferen.. edit.add(pref); menubar.add(edit); setJMenuBar(menubar);
  • 43. About Dialogs JFrame is used for windows, JDialog for dialogs. You can inherit the JDialog. Every dialog needs to know who the host window is dialog belongs to the window
  • 44. Standard Dialogs Really easy way to create dialogs is use standard dialogs JOptionPane JFileChooser JColorChooser ...
  • 45. JOptionPane With one line of code, you can pop up a dialog JOptionPane.showMessageDialog(hostframe, "Hello!"); Also available showConfirmDialog yes/no/cancel showInputDialog prompt user
  • 46. JFileChooser JFileChooser chooser = new JFileChooser(); int returnVal = chooser.showOpenDialog(parent); if(returnVal == JFileChooser.APPROVE_OPTION) String file = chooser.getSelectedFile().getName();