SlideShare a Scribd company logo
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

Awt
AwtAwt
The AWT and Swing
The AWT and SwingThe AWT and Swing
The AWT and Swing
adil raja
 
Awt and swing in java
Awt and swing in javaAwt and swing in java
Awt and swing in java
Shehrevar Davierwala
 
GUI programming
GUI programmingGUI programming
GUI programming
Vineeta Garg
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
Tareq Hasan
 
tL19 awt
tL19 awttL19 awt
tL19 awt
teach4uin
 
Swings
SwingsSwings
Complete java swing
Complete java swingComplete java swing
Complete java swing
jehan1987
 
java swing
java swingjava swing
java swing
Waheed Warraich
 
Awt controls ppt
Awt controls pptAwt controls ppt
Awt controls ppt
soumyaharitha
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
shanmuga rajan
 
Graphical User Interface (Gui)
Graphical User Interface (Gui)Graphical User Interface (Gui)
Graphical User Interface (Gui)
Bilal Amjad
 
Java swing
Java swingJava swing
Java swing
Apurbo Datta
 
Chapter 1 swings
Chapter 1 swingsChapter 1 swings
Chapter 1 swings
Jafar Nesargi
 
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
suraj pandey
 
Java swing
Java swingJava swing
Java swing
Arati Gadgil
 
Graphical User Interface in JAVA
Graphical User Interface in JAVAGraphical User Interface in JAVA
Graphical User Interface in JAVA
suraj pandey
 
Java awt tutorial javatpoint
Java awt tutorial   javatpointJava awt tutorial   javatpoint
Java awt tutorial javatpoint
Ricardo Garcia
 
28 awt
28 awt28 awt
28 awt
Prachi Vijh
 
java2 swing
java2 swingjava2 swing
java2 swing
guest0282b71
 

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

Gu iintro(java)
Gu iintro(java)Gu iintro(java)
Gu iintro(java)
Satish Verma
 
Mental models
Mental modelsMental models
Mental models
aukee
 
Modul oop with java application mauludin
Modul oop with java application   mauludinModul oop with java application   mauludin
Modul oop with java application mauludin
Mauludin 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 Ecosystem
jexp
 
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 Android
Rich Helton
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
Prof. 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 と Gradle
Keishin Yokomaku
 
12 gui concepts 1
12 gui concepts 112 gui concepts 1
12 gui concepts 1
Jomel Penalba
 
OOP in Java
OOP in JavaOOP in Java
OOP in Java
wiradikusuma
 
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
Marcelo Quinta
 
Practical OOP In Java
Practical OOP In JavaPractical OOP In Java
Practical OOP In Java
wiradikusuma
 
java swing
java swingjava swing
java swing
vannarith
 
Gui programming (awt)
Gui programming (awt)Gui programming (awt)
Gui programming (awt)
Ravi_Kant_Sahu
 
Android Development: The Basics
Android Development: The BasicsAndroid Development: The Basics
Android Development: The Basics
Mike Desjardins
 
Contingency theory of management
Contingency theory of managementContingency theory of management
Contingency theory of management
ARUN NAIK
 
Android ppt
Android pptAndroid ppt
Android ppt
Ansh Singh
 
Android for Java Developers
Android for Java DevelopersAndroid for Java Developers
Android for Java Developers
Marko Gargenta
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
Veerabadra Badra
 

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 GUI Programming with Java

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
 
swings.pptx
swings.pptxswings.pptx
swings.pptx
Parameshwar Maddela
 
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
fashionfolionr
 
Programming in java_-_17_-_swing
Programming in java_-_17_-_swingProgramming in java_-_17_-_swing
Programming in java_-_17_-_swing
josodo
 
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
arccreation001
 
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
eyeonsecuritysystems
 
Groovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonGroovy-er desktop applications with Griffon
Groovy-er desktop applications with Griffon
Eric Wendelin
 
Groovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonGroovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With Griffon
Matthew 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.pdf
fashionscollect
 
SWING.pptx
SWING.pptxSWING.pptx
SWING.pptx
SamyakJain710491
 
SwingApplet.pptx
SwingApplet.pptxSwingApplet.pptx
SwingApplet.pptx
GEETHAS668001
 
Swing_Introduction.ppt
Swing_Introduction.pptSwing_Introduction.ppt
Swing_Introduction.ppt
Satyanandaram Nandigam
 
Griffon @ Svwjug
Griffon @ SvwjugGriffon @ Svwjug
Griffon @ Svwjug
Andres Almiray
 
Java awt
Java awtJava awt
Tuto jtatoo
Tuto jtatooTuto jtatoo
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
swapnac12
 
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
 
CORE JAVA-2
CORE JAVA-2CORE JAVA-2
Swing
SwingSwing
Swing
SwingSwing

Similar to GUI Programming with Java (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

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

Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 

Recently uploaded (20)

Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 

GUI Programming with Java

  • 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();