Components and Facilities for Rich Graphical User Interfaces - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Components and Facilities for Rich Graphical User Interfaces

Description:

Learn to add rs, spinners, progress bars, and scrollbars to a GUI application ... The JScrollPane class adds scrollbars automatically to control scrolling ... – PowerPoint PPT presentation

Number of Views:94
Avg rating:3.0/5.0
Slides: 40
Provided by: nort7
Category:

less

Transcript and Presenter's Notes

Title: Components and Facilities for Rich Graphical User Interfaces


1
Components and Facilities for Rich Graphical User
Interfaces
  • Chapter 7

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

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

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

5
Packages of the AWT
6
Packages of the AWT (Cont.)
7
The Swing Packages
8
The Swing Packages (Cont.)
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

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 )

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

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

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 )

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

15
AbstractButton and Its Subclasses
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

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

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

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.

20
Menu Classes in javax.swing
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

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

23
Output of the FontSliderSpinner Class
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

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

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

27
TextComponent and Its Subclasses
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

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

30
Output of the JTree Example Class
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 )

32
The Output of the JTable Class
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

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 )

35
The Page Setup Dialog Box
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

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

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

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
Write a Comment
User Comments (0)
About PowerShow.com