java programming: advanced topics 1 components and facilities for rich graphical user interfaces...

Post on 13-Jan-2016

217 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

top related