Transcript An Introduction to Control Structures
Components and Facilities for Rich Graphical User Interfaces
Chapter 7
Java Programming: Advanced Topics 1
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 2 Java Programming: Advanced Topics
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 3 Java Programming: Advanced Topics
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 4 Java Programming: Advanced Topics
Packages of the AWT
Java Programming: Advanced Topics 5
Packages of the AWT (Cont.)
Java Programming: Advanced Topics 6
The Swing Packages
Java Programming: Advanced Topics 7
The Swing Packages (Cont.)
Java Programming: Advanced Topics 8
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 9 Java Programming: Advanced Topics
JComponent Class
• The JComponent class is the abstract superclass for all Swing components • Methods: – void add
xxx
Listener(
xxx
Listener
object
) – void repaint( ) – void repaint ( long
msec
) – void repaint (int
x,
int
y,
int
height,
int
width
) – void repaint ( long
msec,
int
x,
int
width
)
y,
int
height,
int – void setVisible( boolean
b
) – void update( Graphics
context
) Java Programming: Advanced Topics 10
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 11
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 12
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
) 13 Java Programming: Advanced Topics
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 14
AbstractButton and Its Subclasses
Java Programming: Advanced Topics 15
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 16 Java Programming: Advanced Topics
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 17 Java Programming: Advanced Topics
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 18 Java Programming: Advanced Topics
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.
19 Java Programming: Advanced Topics
Menu Classes in javax.swing
Java Programming: Advanced Topics 20
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 21 Java Programming: Advanced Topics
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 22 Java Programming: Advanced Topics
Output of the FontSliderSpinner Class
Java Programming: Advanced Topics 23
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 24 Java Programming: Advanced Topics
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 25 Java Programming: Advanced Topics
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 26 Java Programming: Advanced Topics
TextComponent and Its Subclasses
Java Programming: Advanced Topics 27
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 28 Java Programming: Advanced Topics
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 29
Output of the JTree Example Class
Java Programming: Advanced Topics 30
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 31
The Output of the JTable Class
Java Programming: Advanced Topics 32
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 33 Java Programming: Advanced Topics
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
) 34 Java Programming: Advanced Topics
The Page Setup Dialog Box
Java Programming: Advanced Topics 35
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 36 Java Programming: Advanced Topics
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 37 Java Programming: Advanced Topics
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 38 Java Programming: Advanced Topics
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 39 Java Programming: Advanced Topics