Java GUI COMP 401 Fall 2014 Lecture 16 10/20/2014.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
Graphical User Interfaces CS 2110 Spring Ivan Sutherland: “Sketchpad”,
User Interface Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
©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.
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.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
GUI Programming in Java
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Welcome to CIS 083 ! Events CIS 068.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
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.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
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 CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Introduction to Swing Components Chapter 14.  Part of the Java Foundation Classes (JFC)  Provides a rich set of GUI components  Used to create a Java.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
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.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
GUI Graphical User Interface. Outline Introduction Packages Components Layout Manager Events and Listeners Examples.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
CS Fall 2012, Lab 09 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /20/2015 GUI - Graphical User Interface.
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.
Programming with Java’s Swing API February 4, 2003 CMPS Advanced Programming Graphical User Interfaces.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Software Design 5.1 From Using to Programming GUIs l Extend model of "keep it simple" in code to GUI  Bells and whistles ok, but easy to use and hide.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
5-Jan-16 R Ramesh Swing. 5-Jan-16 R Ramesh An Introduction to Java Foundation Classes (JFC) A suite of libraries to assist programmers create enterprise.
Creating User Interfaces Event-Driven Programming.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
GUI 1: JFC and Swing Basics OOP tirgul No
Java Swing What is swing? Swing is a ”Lightweight” GUI ToolKit for Java Different from AWT / SWT Toolkits for GUIs Few lines of code to produce GUI elements.
A Quick Java Swing Tutorial
Chapter 14 Event-Driven Programming
Chapter 12 Event-Driven Programming
Advanced User Interfaces
A Quick Java Swing Tutorial
Programming in Java Event Handling
GUI Programming III: Events
Chapter 13: Advanced GUIs and Graphics
Event-driven programming for GUI
A Quick Java Swing Tutorial
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Java GUI COMP 401 Fall 2014 Lecture 16 10/20/2014

Java User Interface Programming AWT – Original Java UI framework – Normalized interface to native OS UI toolkits – Advantages and Disadvantages – Packages start with java.awt Swing – Pure Java – More sophisticated components – More easily extended and customized – Packages start with javax.swing Class names generally start with “J” 2

Hello, World lec16.ex1 3

Hello, World JFrame – Top level window – setTitle() : text in window title – setDefaultCloseOperation() What happens when window is closed. 4 JFrame main_frame = new JFrame(); main_frame.setTitle("Hello, World"); main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Hello, World JPanel – Generic container window. Used to contain other user interface elements. main_frame.setContentPane(main_panel) – Top-level windows have a “content pane” Main area of window. Other areas of window: menu bar, window title, etc. – Replacing default content pane with our Jpanel Accept this as cookbook for now. 5 JPanel main_panel = new JPanel(); main_frame.setContentPane(main_panel);

Hello, World Containers (i.e., JPanel) associated with a “layout” manager. – Determines how user interface elements are arranged within the container. – Different layout managers provide different types and styles of arrangement. Some limit the number and location of component elements. Differ in flexibility and sophistication – BorderLayout Allows 5 components to be placed in one of 5 areas: – NORTH, SOUTH, EAST, WEST, CENTER – Center area is given any extra space. – Unfilled areas are collapsed. 6 main_panel.setLayout(new BorderLayout());

Hello, World JLabel – Simple text component – Property setters for alignment, color, etc. Colors in Java represented by class java.awt.Color – Predefined colors available as class constants. – SwingConstants Defines a number of constants and enumerations used as symbolic names understood by various methods. 7 JLabel hello_world_label = new JLabel("Hello, World!"); hello_world_label.setHorizontalAlignment(SwingConstants.CENTER); hello_world_label.setForeground(Color.BLUE); hello_world_label.setBackground(Color.YELLOW);

Hello, World Every user interface component is “contained” by some parent. – Here we add the label to the main_panel. – Second argument to add method depends on layout manager of container. Different layout managers need/support different kinds of arguments in order to specify where exactly newly added component will go. 8 main_panel.add(hello_world_label, BorderLayout.CENTER);

Hello, World Top-level window must be made visible. – Until now, Java was waiting until we had set everything up. – Call to pack() resolves layout geometry. 9 main_frame.pack(); main_frame.setVisible(true);

Top Level Windows JDialog – Dialog box top-level windows. – Several types pre-defined for ease of use. File choosers, informational, input prompt, etc. JFrame – Normal top-level windows for UI JApplet – We’ll be ignoring this for now. 10

JOptionPane Examples JOptionPane provide quick and easy dialog boxes. – showConfirmDialog Allows user to confirm a choice with a Yes/No/Cancel response. – showInputDialog Prompts for text input. – showMessageDialog Shows message, waits for user to acknowledge. All of these are “modal”. – Flow of execution is halted until dialog is dealt with. – Method returns response directly. lec16.ex2 11

JFrame Non-modal top-level window. – May or may not have a window bar Location of which is operating system and look-and-feel specific – Operation defined by what is placed in it. – General pattern Main method of program sets up UI in a JFrame JFrame made visible. Rest of program now responds asynchronously to user interface events. Hello, world revisited. 12

Containment Hierarchy Recall that a JPanel object used as content pane for top-level window – Root of “containment hierarchy”. – All user interface elements must be placed in a container (e.g., JPanel). – Containers can be nested within containers Groups UI elements together Enables hierarchical sub-layout 13

Layout Managers BorderLayout – 5 areas: NORTH, EAST, WEST, SOUTH, CENTER – N/E/W/S also known as: PAGE_START, LINE_START, LINE_END, PAGE_END BoxLayout – Stacked either horizontally or vertically GridLayout – Equal sized, regular grid. 14

Swing Components Text – JLabel, JTextField, JTextArea Buttons – JButton, JCheckBox, JRadioButton, JToggleButton Sliders – JSlider Lots of others 15

UI Events UI elements respond to interaction by generating “events”. – Listeners are registered to receive notification when a particular event occurs. – Different listener interfaces defined for different kinds of events. Listening method receives event information as a parameter. Should recognize this as observer/observable – UI elements are the observable – Listeners are the observers 16

Button Example lec16.ex3 addActionListener(ActionListener l) – Method common to all types of buttons. – Defined in AbstractButton Parent of all button types. – Serves as the observable’s registration method. ActionListener interface: – void actionPerformed(ActionEvent e) ActionEvent encapsulates all event information – ActionEvent Parent class is AWTEvent – Common information for all event types. – getSource() » Returns reference to object that generated the event. Other information provided depending on the subclass. 17

Java UI’s so far Create a top level window. Create a JPanel to be content pane. Fill JPanel with other components – Buttons, sliders, etc. – Containers with components arranged in them. And so forth. Connect UI events with “listeners” – Listeners take action as response. – Action may change/update UI Make top level window visible. 18

Swing Component Class Hierarchy 19 java.awt.Component java.awt.Container javax.swing.JComponent JPanel JScrollPane JSplitPane JTabbedPane JToolBar JComboBox JLabel JList JProgressBar JSeparator JSlider JToolTip JTree AbstractButton JButton JToggleButton JCheckBox JRadioButton JMenuItem JMenu

Listener Types Supported at awt.Component – ComponentListener – FocusListener – KeyListener – MouseListener – MouseMotionListener – MouseWheelListener Supported at awt.Container – ContainerListener Supported at AbstractButton – ActionListener – ChangeListener – ItemListener Supported by individual components – ActionListener : JComboBox – ChangeListener : JSlider, JProgressBar 20

Keyboard Concepts Keyboard events go to component that currently has “focus”. – setFocusable(boolean status) – isFocusable() – requestFocusInWindow() Java provides a framework for managing focus in more sophisticated ways if necessary. – KeyListener interface – keyTyped(KeyEvent e) – keyPressed(KeyEvent e) – keyReleased(KeyEvent e) Typing of character vs. pressing of key – Basic sequence of events generated: key press, key typed, key released Important KeyEvent methods – char getKeyChar() This is only valid for key typed events. – int getKeyCode() – isAltDown(), isControlDown(), isShiftDown(), isMetaDown() 21

Mouse Concepts MouseListener – mouseClicked(MouseEvent e) – mouseEntered(MouseEvent e) – mouseExited(MouseEvent e) – mousePressed(MouseEvent e) – mouseReleased(MouseEvent e) MouseMotionListener – mouseDragged(MouseEvent e) – mouseMoved(MouseEvent e) MouseEvent – Position info Relative to component: getX(), getY(), getPoint() Absolute position: getXOnScreen(), getYOnScreen() – Click info: getClickCount(), getButton() – Modifiers: isAltDown(), isShiftDown(), etc... 22

Composing Widgets A set of related UI elements that act as a unit within your UI. Basic idea: – Subclass JPanel – In constructor, create and arrange UI components. – Provide methods for attaching listeners, requesting current state of UI, etc. Could be straight delegation or could be mediated by more sophisticated logic. 23

Color Chooser Widget lec16.ex4.v1 – Basic widget construction and set up lec16.ex4.v2 – Wiring up internal behavior lec16.ex4.v3 – Providing external behavior lec16.ex4.v4 – Demonstrating keyboard concepts

Model – View Pattern Keep object representing the data separate from widget providing the UI – Model: Object representing logical entity within your application. – View: Object (widget) representing UI to model. 25 Model View Inform when data changes. Inform when user interacts.