1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

Slides:



Advertisements
Similar presentations
CS18000: Problem Solving and Object-Oriented Programming.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
Corresponds with Chapter 12
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
1 UFCE3T-15-M Object- oriented Design and Programming Block 3 GUI Programming Jin Sa.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
GUI and event-driven programming An introduction.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Advanced Java and Android Programming Day 1: Introduction to GUI Programming Intro to GUI Programming1.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Layout Managers Arranges and lays out the GUI components on a container.
GUI Graphical User Interface. Outline Introduction Packages Components Layout Manager Events and Listeners Examples.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
University of Limerick1 Software Architecture Java Layout Managers.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
1 Java GUIs Dr. Randy M. Kaplan. 2 Constructing User Interfaces From a functionality standpoint there are two things that every program must implement.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
GUI Basics. Agenda What GUI How to make in java Creating frames Frequently used GUI components Layout Managers.
Computer Science 209 GUIs Model/View/Controller Layouts.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
1 Lecture 8: User Interface Components with Swing.
Introduction to GUI in 1 Graphical User Interface 3 Nouf Almunyif.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Object-Orientated Analysis, Design and Programming
Swing JComponents.
Modern Programming Language Java
Ellen Walker Hiram College
Chapter 13: Advanced GUIs and Graphics
Chapter 10 Getting Started with Graphics Programming
GUI building with the AWT
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1

2 / 67 Graphical-User Interfaces (GUIs)

3 / 67 So far...  Only created text-based programs  No fancy graphics (buttons! menus! text-fields!)

4 / 67 Graphical-User Interfaces (GUI)  Going to look at how to create GUIs in Java

5 / 67 Graphical-User Interfaces (GUI)  Going to look at how to create GUIs in Java  Use the SWING API (for desktop-programs)

6 / 67 Graphical-User Interfaces (GUI)  Going to look at how to create GUIs in Java  Use the SWING API (for desktop-programs)  Use the AWT API (for Java Applets – on the web)

7 / 67 The SWING API  Used to create desktop applications.

8 / 67 The SWING API  Used to create desktop applications.  Uses the Model-View-Controller software engineering design pattern.

9 / 67 Model-View-Controller design  Model:  Manages the behavior and data of the application.  Changes state.

10 / 67 Model-View-Controller design  Model:  Manages the behavior and data of the application.  Changes state.  View:  Renders the model into a form for interaction. (Button, textbox, checkbox, etc.)

11 / 67 Model-View-Controller design  Model:  Manages the behavior and data of the application.  Changes state.  View:  Renders the model into a form for interaction. (Button, textbox, checkbox, etc.)  Controller:  Receives user input and initiates a response by interacting with the model.

12 / 67 The SWING API  Example: Scrabble

13 / 67 The GUI API  Use the NetBeans IDE for easy drag-and-drop creation.

14 / 67 The GUI API  Use the NetBeans IDE for easy drag-and-drop creation.  But we are going to focus on basic code

15 / 67 The GUI API  3 Groups of classes:  Component Classes:  Buttons, Labels, TextFields, etc.

16 / 67 The GUI API  3 Groups of classes:  Component Classes:  Buttons, Labels, TextFields, etc.  Container Classes:  Frames, Panels, Applets, etc.

17 / 67 The GUI API  3 Groups of classes:  Component Classes:  Buttons, Labels, TextFields, etc.  Container Classes:  Frames, Panels, Applets, etc.  Helper Classes:  Graphics, Color, Font, etc.

18 / 67 The SWING API  Simple Window  Represented by the JFrame class

19 / 67 The SWING API import javax.swing.JFrame; public static void main(String[] args) { JFrame frame = new JFrame(“A Title”); frame.setSize(400, 300); frame.setLocation(10, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }

20 / 67 The SWING API – add components import javax.swing.*; public static void main(String[] args) { JFrame frame = new JFrame(“A Title”); JButton button = new JButton(“OK”); frame.add(button); frame.setSize(400, 300);... }

21 / 67 The SWING API – add components  A JFrame containts a content pane.

22 / 67 The SWING API – add components  A JFrame containts a content pane.  Content pane = instance of java.awt.Container;

23 / 67 The SWING API – add components  A JFrame containts a content pane.  Content pane = instance of java.awt.Container;  Objects are added to it  frame.add( Component )

24 / 67 The SWING API – add components  A JFrame containts a content pane.  Content pane = instance of java.awt.Container;  Objects are added to it  frame.add( Component )  frame.remove( Component )

25 / 67 Layout Managers  Components in content pane are laid out by layout managers.

26 / 67 Layout Managers  Components in content pane are laid out by layout managers.  Multiple types:  FlowLayout  GridLayout  BorderLayout

27 / 67 Layout Managers - FlowLayout  Components arranged left to right in order.  One row fills up, a new row is started

28 / 67 Layout Managers - FlowLayout  java.awt.FlowLayout  Properties:  alignment: int (CENTER/LEFT/RIGHT/etc.)  hgap, vgap: int (the gaps – default: 5 pixels)

29 / 67 Layout Managers - FlowLayout  java.awt.FlowLayout  Properties:  alignment: int (CENTER/LEFT/RIGHT/etc.)  hgap, vgap: int (the gaps – default: 5 pixels)  Constructors:  FlowLayout()  FlowLayout(alignment, hgap, vgap)

30 / 67 Layout Managers - FlowLayout public class ShowFlowLayout extends JFrame { public ShowFlowLayout() { // set the flow layout setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20); add(new JButton(“Button”)); add(new JTextField(8)); }

31 / 67 Layout Managers - GridLayout  Arrange components in a grid (matrix) formation.  Placed left-to-right.

32 / 67 Layout Managers - GridLayout  Arrange components in a grid (matrix) formation.  Placed left-to-right.  Properties:  rows, columns: int  hgap, vgap: int (the gaps – default: 5 pixels)

33 / 67 Layout Managers - GridLayout public class ShowGridLayout extends JFrame { public ShowGridLayout() { // set the Grid layout – 3 rows, 2 columns setLayout(new GridLayout(3, 2, 10, 10); add(new JButton(“Button”)); add(new JTextField(8)); }

34 / 67 Layout Managers - BorderLayout  Default Layout for ContentPanes (Jframe)  Divides container into 5 areas:  East, West, South, North, Center  Components are added into one of these areas  Properties:  hgap, vgap: int (the gaps – default: 5 pixels)

35 / 67 Layout Managers - BorderLayout public class ShowBorderLayout extends JFrame { public ShowBorderLayout() { // set the Border Layout setLayout(new BorderLayout(10, 10); add(new JButton(“Button”), BorderLayout.EAST); add(new JTextField(8), BorderLayout.WEST); }

36 / 67 The SWING API  Looked at adding Components to the Window (Frame).  And how to lay them out.

37 / 67 The SWING API  Looked at adding Components to the Window (Frame).  And how to lay them out.  But often need “sub-windows” to create more complex interfaces.

38 / 67 Using JPanels as Subcontainers  Panels are subcontainers.

39 / 67 Using JPanels as Subcontainers  Panels are subcontainers.  Can add components to them

40 / 67 Using JPanels as Subcontainers  Panels are subcontainers.  Can add components to them  Also set layouts (default: FlowLayout)

41 / 67 Using JPanels as Subcontainers  Panels are subcontainers.  Can add components to them  Also set layouts (default: FlowLayout) JPanel panel = new JPanel(); panel.add(new JButton(“OK”));

42 / 67 Using JPanels // set the Border Layout of the JFrame setLayout(new BorderLayout(10, 10); // add a Panel to the West and East JPanel p1 = new JPanel(); add(p1, BorderLayout.WEST); JPanel p2 = new JPanel(); p2.setLayout(new GridLayout(2, 2, 5, 5)); add(p2, BorderLayout.EAST); // add components to the east panel p2.add(new JTextField(8)); p2.add(new JButton(“Button1”)); p2.add(new JTextField(8)); p2.add(new JButton(“Button2”)); // one button to the west panel p1.add(new JButton(“Button3”));

43 / 67 Adding Components  Need to add components for interaction.

44 / 67 Adding Components  Need to add components for interaction.  Some useful ones:  JButton  JTextField  JCheckBox  JComboBox  JMenuBar  etc...

45 / 67 Model-View-Controller design  Model: (ALREADY IMPLEMENTED)  Manages the behavior and data of the application.  Changes state.  View: DONE  Renders the model into a form for interaction. (Button, textbox, checkbox, etc.)  Controller:  Receives user input and initiates a response by interacting with the model.

46 / 67 Interaction and Events  Need to handle events from various GUI components.

47 / 67 Interaction and Events  Need to handle events from various GUI components.  Button clicks, text field changes, menu selection, etc.

48 / 67 Interaction and Events  Need to handle events from various GUI components.  Button clicks, text field changes, menu selection, etc.  Event-driven programming

49 / 67 Interaction and Events  Components generate different kinds of Events

50 / 67 Interaction and Events  Components generate different kinds of Events  ActionEvent, ItemEvent, ChangeEvent, MouseEvent, KeyEvent

51 / 67 Interaction and Events  Components generate different kinds of Events  ActionEvent, ItemEvent, ChangeEvent, MouseEvent, KeyEvent  Example: JButton generates ActionEvent

52 / 67 Interaction and Events  Components generate different kinds of Events  ActionEvent, ItemEvent, ChangeEvent, MouseEvent, KeyEvent  Example: JButton generates ActionEvent  Mouse pressed/moved/dragged: MouseEvent

53 / 67 Interaction and Events  Components generate different kinds of Events  ActionEvent, ItemEvent, ChangeEvent, MouseEvent, KeyEvent  Example: JButton generates ActionEvent  Mouse pressed/moved/dragged: MouseEvent  JCheckBox: ItemEvent, ActionEvent

54 / 67 Interaction and Events  Need to “listen” for Events.  Source object fires an event, and an object interested in the event handles it.  Latter object called a “listener”

55 / 67 Interaction and Events  For an object to be a listener, it needs to implement an interface.

56 / 67 Interaction and Events  For an object to be a listener, it needs to implement an interface.  Interface should correspond to the Event.  ActionListener for ActionEvent  MouseListener for MouseEvent  KeyListener for KeyEvent

57 / 67 Interaction and Events  For an object to be a listener, it needs to implement an interface.  Interface should correspond to the Event.  ActionListener for ActionEvent  MouseListener for MouseEvent  KeyListener for KeyEvent object.addXListener(...)

58 / 67 ActionListener interface class AClass implements ActionListener { public void actionPerformed(ActionEvent e) { // do whatever you want }

59 / 67 ActionListener interface class AClass implements ActionListener { public void actionPerformed(ActionEvent e) { // do whatever you want } JButton button = new JButton(“OK”); button.addActionListener(new AClass());

60 / 67 ActionListener interface  ActionEvent methods:

61 / 67 ActionListener interface  ActionEvent methods:  Object getSource(): returns the object on which the Event initially occurred. (in java.util.EventObject)

62 / 67 ActionListener interface  ActionEvent methods:  Object getSource(): returns the object on which the Event initially occurred. (in java.util.EventObject)  String getActionCommand(): returns the command string (text of the button for ex.)

63 / 67 ItemListener interface Used for check boxes, toggle buttons, etc. class AClass implements ItemListener { public void itemStatechanged(ItemEvent e) { // do whatever you want }

64 / 67 ItemListener interface  ItemEvent methods:  Object getItem(): Component-specific object.  int getStateChange(): The new state of the associated object.

65 / 67 Interaction and Events  Look at some other useful Events:  MouseEvent  KeyEvent  WindowEvent

66 / 67 Interaction and Events  Look at some other useful Events:  MouseEvent  KeyEvent  WindowEvent  Experiment!

67 / 67 Summary  Windows and Panels  Layouts  Add Components  Add Listeners  Handle Events