Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
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 program.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces.
Chapter 11 Creating User Interfaces F What is JavaBean? F JComponent F JButton  ImageIcon  JLabel  JTextField  JTextArea  JComboBox  JList  JCheckBox.
Java Software Development Paradigm Lecture # 12. Basics of GUI.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
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.
 What are the basic GUI components we’ve learned so far? › JFrame › JPanel › JButton › JLabel › JTextArea › JTextField › JCheckBox › JRadioButton › Paint.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
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.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
©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.
Introduction To Form Builder
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Graphical User Interface (GUI) Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Advanced Java Class GUI, part 2. JComponent methods addXXXListener(XXXListener) repaint() – [optional arguments: delay and coordinates of sub-area to.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
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.
CS377A: A Programming Approach to HCI Jan Borchers Spring Swing Refresher David Merrill 5/14/2002
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
3461A Readings from the Swing Tutorial. 3461A Overview  The follow is the Table of Contents from the trail “Creating a GUI with JFC/Swing” in the “The.
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.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Graphical User Interface Components Chapter What You Will Learn Text Areas Text Areas Sliders Sliders Menus Menus –With frames –Pop up menus Look.
CSE 219 Patterns in Programming More Design Patterns.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
Java Swing. Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to the familiar.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
1 Java Swing - Lecture 2 Components and Containment Boriana Koleva
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
CSCI Swing1 The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness –The Abstract.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Swing / Session1 / 1 of 30 Module Introduction Distributed Computing in Java.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Graphical User Interfaces Java Foundations: Introduction to Programming.
Swinging in Your Java Playground. Background Swing is a part of the Java Foundation Classes (JFC). The JFC is made up of features intended to give a programmer.
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
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.
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.
Copyright © Curt Hill More Widgets In Abstract Window Toolbox.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 15 : Swing III King Fahd University of Petroleum & Minerals College of Computer.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
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:
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
A Quick Java Swing Tutorial
A Quick Java Swing Tutorial
Chapter 13: Advanced GUIs and Graphics
A Quick Java Swing Tutorial
Chapter 17 Creating User Interfaces
Chapter 17 Creating User Interfaces
Advanced GUIs and Graphics
Presentation transcript:

Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7

Java Programming: Advanced Topics 2 Objectives Become familiar with the extensive library of GUI components offered by the Swing API Write code that uses the methods of the javax.swing.JComponent class Learn to add windows, dialog boxes, and panels to a GUI application Learn to add labels, buttons, and check boxes to a GUI application Learn to add menus, toolbars, and actions to a GUI application Learn to add sliders, spinners, progress bars, and scrollbars to a GUI application

Java Programming: Advanced Topics 3 Objectives (Cont.) Learn to add lists and combo boxes to a GUI application Learn to add text-entry components to a GUI application Learn to add file and color choosers to a GUI application Learn to add tables and trees to a GUI application Add printing support to a GUI application using the 2D API Learn how to discover what print facilities are available to an application using the Java Print Service API

Java Programming: Advanced Topics 4 Programming with the JFC To create applets or application that have graphical user interfaces (GUIs), use the Java Foundation Classes (JFC) of the Java platform Support for the development of GUIs for the Java platform is provided by a combination of the AWT and Swing APIs

Java Programming: Advanced Topics 5 Packages of the AWT

Java Programming: Advanced Topics 6 Packages of the AWT (Cont.)

Java Programming: Advanced Topics 7 The Swing Packages

Java Programming: Advanced Topics 8 The Swing Packages (Cont.)

Java Programming: Advanced Topics 9 Swing API Components Windows Dialog boxes Panels Labels Buttons Check Boxes Menus Toolbars Sliders Spinners Progress bars Scrollbars Lists Combo boxes Text-entry components Tables Trees File and color choosers

Java Programming: Advanced Topics 10 JComponent Class The JComponent class is the abstract superclass for all Swing components Methods: –void addxxxListener(xxxListener object ) –void repaint( ) –void repaint ( long msec) –void repaint (int x, int y, int height, int width) –void repaint ( long msec, int x, int y, int height, int width) –void setVisible( boolean b ) –void update( Graphics context )

Java Programming: Advanced Topics 11 Windows, Dialog Boxes, and Panels Containers: components that hold other components Swing provides several top-level container components: –JApplet –JDialog –JFrame –JWindow To add a component to a JApplet, JDialog, JFrame, or JWindow object, call the method getContentPane

Java Programming: Advanced Topics 12 Windows, Dialog Boxes, and Panels (Cont.) An application runs in a JFrame object An applet runs in a JApplet object You build a GUI adding containers and components to the JFrame object of an application or the panel of an applet JDesktopPane, JInternalFrame, JOptionPane, and JPanel are lightweight components that extend JComponent

Java Programming: Advanced Topics 13 Methods of the Container Class The Container class provides several methods for adding and removing components or working with layouts: –void add( Component comp ) –void add( Component comp, Object constraint ) –Component[ ] getComponents() –LayoutManager getLayout() –void remove( Component comp ) –void setLayout( LayoutManager mgr )

Java Programming: Advanced Topics 14 Labels, Buttons, and Check Boxes Labels add text that typically explains the purpose of the other elements of a user interface Buttons and check boxes provide the opportunity to make selections and trigger actions

Java Programming: Advanced Topics 15 AbstractButton and Its Subclasses

Java Programming: Advanced Topics 16 Menus, Toolbars, and Actions To add menus to your GUI, use the classes: –JMenuBar –JMenuItem –Jmenu –JPopupMenu –JRadioButtonMenuItem –JCheckBoxMenuItem Swing provides support for toolbars through the JToolBar class

Java Programming: Advanced Topics 17 Menus, Toolbars, and Actions (Cont.) Swing provides a mechanism, the Action interface, for defining objects that can be put into toolbars and menus Actions that are common to both menus and toolbars can be created as subclasses of AbstractAction to provide a single point of control

Java Programming: Advanced Topics 18 Menus, Toolbars, and Actions (Cont.) To create a main menu for an application, use a JMenuBar object Use the add method to add menus to the menu bar and the setJMenuBar method to add the menu bar to its window For a menu that is associated with a component and pops up when the user clicks a component, create a JPopupMenu object

Java Programming: Advanced Topics 19 Menus, Toolbars, and Actions (Cont.) For every item in a menu, use an instance of JMenuItem or a subclass of JMenuItem To nest menus, use JMenu objects. A JMenu object is a JMenuItem object that is itself a menu For a menu item that is also a check box, use a JCheckBoxMenuItem object For a menu item that is also a radio button, use a JRadioButtonMenuItem object.

Java Programming: Advanced Topics 20 Menu Classes in javax.swing

Java Programming: Advanced Topics 21 Sliders, Spinners, Progress Bars, and Scrollbars JProgressBar, JScrollBar, JSlider, and JSpinner allow users to select or display a value within a bounded range Programs use JProgressBar objects to indicate the progress of a long-running operation Use a JScrollBar object to control the visible contents of a component

Java Programming: Advanced Topics 22 Sliders, Spinners, Progress Bars, and Scrollbars (Cont.) Use a JScrollPane object to display a component with contents that are likely to exceed the available visible area The JScrollPane class adds scrollbars automatically to control scrolling operations Use a JSlider object to allow selection of a value within a bounded range Use a JSpinner object to allow selection of a value within a bounded range from a predetermined list of legal values

Java Programming: Advanced Topics 23 Output of the FontSliderSpinner Class

Java Programming: Advanced Topics 24 Lists and Combo Boxes The JComboBox component is a drop-down list that is useful for lists with a small number of items A JList object displays a list of choices from which it is possible to select one or multiple items JList objects can be contained within JScrollPane and scrolled to view the entire list

Java Programming: Advanced Topics 25 Text-Entry Components A JEditorPane object is a region that is designed to parse and edit specific types of structured text content A JFormattedTextField object is an entry field in which the data that the user enters must meet the specified formatting rules A JPasswordField field can display a single line of text and lets the user enter or edit the text

Java Programming: Advanced Topics 26 Text-Entry Components (Cont.) JTextArea is a region that can contain several lines of text JTextField can display a single line of text and lets the user enter or edit the text A JTextPane object is a region that can contain several lines of text

Java Programming: Advanced Topics 27 TextComponent and Its Subclasses

Java Programming: Advanced Topics 28 Color and File Choosers Swing includes the standard classes JFileChooser and JColorChooser that implement panels and that can be embedded in dialogs or internal frames JColorChooser is a panel that allows users to browse a palette of available colors and select one JFileChooser is a panel that allows users to browse the contents of the file system and select a file to open or a file to save

Java Programming: Advanced Topics 29 Tables and Trees Swing provides two sophisticated controls for structured information: –JTable –JTree The JTable class is best suited for tabular information The JTree class is ideal for hierarchical information

Java Programming: Advanced Topics 30 Output of the JTree Example Class

Java Programming: Advanced Topics 31 The JTable Constructors Table() Table(int rows, int columns ) Table( Object[][] rowData, Object[] columnNames ) Table( Vector rowData, Vector columnNames ) Table( TableModel model ) Table( TableModel model, TableColumnModel tcModel ) Table( TableModel model, TableColumnModel tcModel, ListSelectionModel lsModel )

Java Programming: Advanced Topics 32 The Output of the JTable Class

Java Programming: Advanced Topics 33 Printing with the 2D API The Java 2D API introduced the java.awt.print package to provide printing support on the Java platform The java.awt.print.PrinterJob class provides the control point for displaying print-related dialog boxes, indicating what will print, and actually starting the print job The PageFormat class provides information about the size, orientation, and area available for printing for a page of printed output

Java Programming: Advanced Topics 34 java.awt.print.PrinterJob Methods of the class java.awt.print.PrinterJob: –PageFormat defaultPage( ) –PrintService getPrintService() –PageFormat pageDialog( PageFormat format ) –PageFormat pageDialog( PrintRequestAttributeSet attrs ) –void print() –void print( PrintRequestAttributeSet attrs ) –boolean printDialog() –boolean printDialog( PrintRequestAttributeSet attrs ) –void setPrintable( Printable painter )

Java Programming: Advanced Topics 35 The Page Setup Dialog Box

Java Programming: Advanced Topics 36 Java Print Service API The Java Print Service API can be used for printing text and graphics that are displayed within an application The Java Print Service API is used to handle many flavors of documents, to search and discover print services for handling these document flavors

Java Programming: Advanced Topics 37 Java Print Service API (Cont.) The Doc interface encapsulates the interface for an object that supplies a piece of print data for a print job The DocFlavor class encapsulates the format in which data will be supplied to a DocPrintJob object The javax.print.DocPrintJob interface represents a print job that can print a specified document with a set of job attributes

Java Programming: Advanced Topics 38 Summary The AWT API provides the infrastructure for layout managers, events, event listeners, and basic 2D graphics support The Swing API provides a comprehensive set of GUI components to produce professional applications JComponent is the superclass of all Swing components, and JApplet, JDialog, JFrame, and JWindow are the top-level containers An application runs in a JFrame object, and an applet runs in a JApplet object

Java Programming: Advanced Topics 39 Summary (Cont.) Swing provides classes to support buttons, check boxes, menus, toolbars, sliders, spinners, progress bars, scrollbars, lists, combo boxes, text-entry components Swing includes the standard classes JFileChooser and JColorChooser that implement panels JTable is used for tabular information, and JTree for hierarchical information Print capability can be added to an application by using either the Printing API from the 2D API or by using the Java Print Service API