©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 14 - 1 Chapter 14 GUI and Event-Driven Programming.

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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 : Advanced GUI  Mouse Events  JPanels  Layout Managers 
Event Handling Events and Listeners Timers and Animation.
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.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 : Event-Driven Programming and GUI Objects.
Chapter 7 Event-Driven Programming and Basic GUI Objects.
©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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 14 GUI and Event-Driven Programming.
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 and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
GUI and event-driven programming An introduction.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
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.
Using Inheritance to Customize Frames Use inheritance for complex frames to make programs easier to understand Design a subclass of JFrame Store the components.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Graphical User Interface in Java
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– 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.
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.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Layout Managers Arranges and lays out the GUI components on a container.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
AGDER COLLEGEFACULTY OF ENGINEERING & SCIENCE GUI Components ikt403 – Object-Oriented Software Development.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
DCS2133 Object Oriented Programming Graphical User Interface & Event-Driven Programming.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-2 ( Book Chapter 14) GUI and Event-Driven Programming.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
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.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
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.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Christopher Budo, Davis Nygren, spencer franks, Luke miller
Advanced User Interfaces
Graphical User Interface (pronounced "gooey")
Chapter 13: Advanced GUIs and Graphics
Creating Graphical User Interfaces
Graphical User Interface
Chapter 7-3 (Book Chapter 14)
GUI and Event-Driven Programming
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

©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 What is a GUI program? Window-based –contains menus, buttons, text fields, … User interacts with program using mouse as well as keyboard User determines order in which things get done

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Graphical User Interfaces Implemented by using classes from two packages. –javax.swing –java.awt GUI is constructed from Components –Top-level component is a Container which can hold other components –Light-weight containers can be used to organize other components –Simple components include buttons, text boxes, menus, etc. LayoutManager controls how Components are laid out within a container

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter JFrame The JFrame class contains rudimentary functionalities to support features found in any frame window. A JFrame is a Container The main window for a GUI applicationis generally a JFrame –One approach is to use the JFrame class and add custom components to it –For more flexibility, we can define a subclass of the JFrame class.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Creating a Subclass public class JFrameSubclass extends JFrame { public JFrameSubclass { // set up the JFrame // add components // implement desired behavior }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter The Content Pane of a JFrame A JFrame is a Container but we don't add Components to it directly. Every JFrame has a ContentPane –The content pane is where we put GUI objects such as buttons, labels, scroll bars, and others. –We access the content pane by calling the frame’s getContentPane() method. –We can use setContentPane() to use our own container classes (extending JPanel) instead of the default one A JFrame also has a MenuBar –We can add Menus to the MenuBar –We can add MenuItems and other Menus to a Menu

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Containers JPanel - use with a layout manager to organize other components JScrollPane - provides scrollbars for a single component JSplitPane - a container that holds two components –Putting a JSplitPane in each section allows for more components JTabbedPane - layers multiple components

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Adding Components to a Container The Container classes have an add method that can be used to add Components. Container contentPane = getContentPane(); JButton okButton = new JButton("OK"); contentPane.add(okButton); contentPane.add( new JLabel( "label text")); JTextField inputBox = new JTextField( numChars); contentPane.add(inputBox); Components generally appear in the order they are added –Component placement is controlled by a layout manager

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Components Buttons –JButton can display text and or image –JCheckBox has two states Text Components –JTextField –JTextArea –JLabel Components for selection –JRadioButton and ButtonGroup –JComboBox –JList Others –JSlider –JSpinner

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism to process events is called event handling. The event-handling model of Java is based on the concept known as the delegation-based event model. With this model, event handling is implemented by two types of objects: –event source objects –event listener objects

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Event Source Objects An event source is a GUI object which generates events. –Buttons, text boxes, list boxes, and menus are common event sources in GUI-based applications. An event listener object is an object that includes a method that gets executed in response to the generated events. –A listener must be registered, to a source, so it can be notified when the source generates events. –Use an interface to guarantee that the listener object has the appropriate method

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Connecting Source and Listener JButton Handler event source event listener notify register A listener must be registered to a event source. Once registered, it will get notified when the event source generates events.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Registration and notification are specific to event types MouseListener handles mouse events ItemListener handles item selection events ActionListener handles button and menu events Event listeners implement a listener interface public class ButtonHandler implements ActionListener {... } Register a Listener with an event generator with an add___Listener method JButton button = new JButton("OK"); ButtonHandler handler = new ButtonHandler( ); button.addActionListener(handler); Event Types

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Listener Interfaces A Java interface includes only constants and abstract methods. A Java interface specifies a behavior. A class implements an interface by providing the method body to the abstract methods stated in the interface. –Any class can implement the interface. The ActionListener interface includes one method named actionPerformed. –A class that implements the ActionListener interface must define actionPerformed. –This is where we put a code we want to be executed in response to the generated events.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Two Approaches to Listeners Define a separate class –ButtonHandler and Ch14JButtonEvents –A separate class doesn't have access to the data of the JFrame class unless it is defined inside the JFrame class Let the JFrame implement the listener interface public class Ch14JButtonFrameHandler extends JFrame implements ActionListener {... public void actionPerformed(ActionEvent event) { JButton clickedButton = (JButton) event.getSource(); // code to implement the desired response }}

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Component Classes for Handling Text The Swing GUI classes JLabel, JTextField, and JTextArea deal with text. A JLabel object displays uneditable text (or image). A JTextField object allows the user to enter a single line of text. –generates ActionEvent when user presses return key A JTextArea object allows the user to enter multiple lines of text. It can also be used for displaying multiple lines of uneditable text. –Use the setText and append methods to control content –If the JTextArea is editable, user can type into it

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Using Scroll Bars A JTextArea does not have scroll bars. To allow scrolling of any component, we add the component to a JScrollPane. JTextArea textArea = new JTextArea();... JScrollPane scrollText = new JScrollPane(textArea);... contentPane.add(scrollText);

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Other Common GUI Components JCheckBox (generates ItemEvent and ActionEvent) –see Ch14JCheckBoxSample1.java and Ch14JCheckBoxSample2.java JRadioButton (generates ItemEvent and ActionEvent, use in conjunction with ButtonGroup) –see Ch14JRadioButtonSample.java JComboBox (generates ItemEvent and ActionEvent) –see Ch14JComboBoxSample.java Jlist (generates ActionEvent) –see Ch14JListSample.java Jslider (generates ChangeEvent) –see Ch14JSliderSample.java

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Layout Managers The layout manager determines how the GUI components are added to the container (such as the content pane of a frame) Among the many different layout managers, the common ones are –FlowLayout (see Ch14FlowLayoutSample.java) –BorderLayout (see Ch14BorderLayoutSample.java) –GridLayout (see Ch14GridLayoutSample.java) –BoxLayout (see guiExtras/BoxLayoutSample.java)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter FlowLayout In using this layout, GUI componentsare placed in left- to-right order. –When the component does not fit on the same line, left-to- right placement continues on the next line. As a default, components on each line are centered. When the frame containing the component is resized, the placement of components is adjusted accordingly. Creating a FlowLayout contentPane.setLayout( new FlowLayout());

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter BorderLayout This layout manager divides the container into five regions: center, north, south, east, and west. –The north and south regions expand or shrink in height only –The east and west regions expand or shrink in width only –The center region expands or shrinks on both height and width. Not all regions have to be occupied. Creating a BorderLayout – contentPane.setLayout( new BorderLayout()); Only one component can be added to each area – contentPane.add( component, BorderLayout.CENTER);

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter GridLayout This layout manager placesGUI components on equal- size N by M grids. Components are placed in top-to-bottom, left-to-right order. The number of rows and columns remains the same after the frame is resized, but the width and height of each region will change. Creating a GridLayout contentPane.setLayout( new GridLayout(rows, cols));

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter BoxLayout BoxLayout is in the swing package It allows you to arrange components either horizontally (BoxLayout.X_AXIS) or vertically (BoxLayout.Y_AXIS). The constructor needs a reference to the container contentPane.setLayout( new BoxLayout( contentPane, BoxLayout.X_AXIS)); Use Box class to create space between components NOTE: This is not in your text

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Nesting Containers It is not always easy to place all GUI components on a single JPanel and get them organized in a pleasing way. Containers can be nested so that different areas can be organized differently. –Ch14NestedPanels1.java provides the user interface for playing Tic Tac Toe. –Ch14NestedPanels2.java provides the user interface for playing HiLo. JSplitPane is a container that holds exactly two components –The two sections can be side-by-side or one above the other –See guiExtras/SplitFrame.java

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Menus The javax.swing package contains three menu-related classes: JMenuBar, JMenu, and JMenuItem. JMenuBar is a bar where the menus are placed. There is one menu bar per frame. JMenu (such as File or Edit) is a group of menu choices. JMenuBar may include many JMenu objects. JMenuItem (such as Copy, Cut, or Paste) is an individual menu choice in a JMenu object. Only the JMenuItem objects generate events.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Sequence for Creating Menus 1.Create a JMenuBar object and attach it to a frame. 2.Create a JMenu object. 3.Create JMenuItem objects and add them to the JMenu object. 4.Attach the JMenu object to the JMenuBar object.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter File Handling in a GUI Program The JFileChooser class (javax.swing package) has methods that create dialog boxes for selecting files public int showOpenDialog( Component parent) public int showSaveDialog( Component parent) –return value indicates which buton was used to close the dialog –Use chooser.getSelectedFile() to get a reference to the selected file By default, the dialogs show the user's home directory You can specify a path in the constructor public JFileChooser( File currentDirectory)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Handling Mouse Events Mouse events include such user interactions as –moving the mouse –dragging the mouse (moving the mouse while the mouse button is being pressed) –clicking the mouse buttons. The MouseListener interface handles mouse button –mouseClicked, mouseEntered, mouseExited, mousePressed, and mouseReleased The MouseMotionListener interface handles mouse movement –mouseDragged and mouseMoved. See Ch14TrackMouseFrame and Ch14SketchPad

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter etc. Key Events Timer