Advanced GUI Components - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Advanced GUI Components

Description:

how to do split panes and tabbed panes. how to create MDI (multiple ... toolBarTitle = Navigation des Pages sur la Toile # tooltip for forward toolbar button ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 21
Provided by: cyndi8
Category:

less

Transcript and Presenter's Notes

Title: Advanced GUI Components


1
Chapter 2
  • Advanced GUI Components

2
Topics
  • how to render HTML and Rich Text Format (RTF)
    pages
  • how to do split panes and tabbed panes
  • how to create MDI (multiple-document interfaces)
  • how to do drag and drop
  • how to use the Command Design Pattern (Action
    Interface)
  • what Java mechanisms exist for multiple languages
    and countries
  • what Java mechanisms exist for disabled users
    (not covered)

3
HTML and the JEditor Class
  • JTextComponent - base class for other text
    components
  • JTextField - single-line text component used for
    simple user input, displaying calculation
    results, etc.
  • JPassword, subclass of JTextField, used for
    obtaining passwords.
  • JTextArea provides larger visible area, used for
    plain-text documents.
  • JEditorPane provides enhanced text-rendering
    capabilities.  Can render html and rtf (rich text
    format) documents.  Enables hyperlinks if text is
    not editable make your own web browser!
  • JTextPane renders only styled documents, not
    plain text.  Use for fine control over visual
    display of document.

4
Enhanced Pane Options
  • JSplitPane provides a divider which users can
    reposition. 
  • JTabbedPane uses folder-style tabs to provide
    access to different panels.
  • JScrollPane provides a container that enables
    scrolling

5
Multiple-Document Interfaces
  • Multiple Document Interfaces (MDI) allow users to
    view multiple documents in a single application.
  • Each document appears in a separate window.
  • JDesktopPane and JInternalFrame are used to build
    MDI applications.
  • JDesktopPane is the application's "desktop."
  • JInternalFrame is similar to JFrame, with a title
    bar and buttons to iconify etc. 

6
Swing toolbars
  • JToolbar allows toolbars to be added to GUI
  • Can modify appearance, "dock" toolbar on top,
    side or bottom, can be a floating toolbar. 
  • A toolbar is a container, so it can contain other
    GUI components. 
  • Orientation specifies how child components are
    arranged.  Default is horizontal. 

7
Command design pattern/Action Interface
  • Design issue it's good for an application to
    provide multiple ways to perform a function
    (e.g., menu, toolbar, pop-up menu).  BUT, don't
    want to repeat functionality in multiple places.
  • Solution define functionality once in a reusable
    object that can be accessed via menu, toolbar,
    etc. 
  • Action interface defines required methods for
    Command design pattern.
  • Extends ActionListener so it can process
    ActionEvents generated by GUI components.
  • Uses an actionPerformed method that is invoked
    when the user requests an action (e.g., by
    clicking a button). 
  • Can add an Action to a JMenu, which causes the
    JMenu to create a JMenuItem, using the Action
    properties to configure the JMenuItem. 
  • Ex fileMenu.add(exitAction)
  • // creates menu item with properties determined
    by exitAction
  • Can enable/disable all GUI components by
    enabling/disabling the Action.  Action items are
    enabled by default.  Can use setEnabled to change.

8
Command/Action cont.
  • Class AbstractAction implements Action. It's
    abstract because actionPerformed is not defined. 
    It also implements Cloneable and Serializable.
  • Each Action has properties (key/value
    combination).  The keys are
  • NAME (component label)
  • SHORT_DESCRIPTION (tool tips)
  • SMALL_ICON
  • MNEMONIC_KEY (menu items)
  • ACCELERATOR_KEY (Ctrl)
  • ACTION_COMMAND_KEY (retrieves command string used
    in ActionEvents),
  • LONG_DESCRIPTION (e.g., for help).
  • Use putValue to set properties.

9
Drag and Drop
  • Two Java APIs used in drag-and-drop data
    transfers
  • java.awt.datatransfer enables copying and moving
    within a single application.
  • Data can be transferred from a data source to a
    data target.
  • API enables programs to recognize drag-and-drop
    gestures.
  • Class DropTarget is associated with a Component
    via a call to the DropTarget constructor.   Other
    optional parameters to the constructor include an
    integer that represents acceptable actions, a
    DropTargetListener and a boolean that determines
    whether the target is currently accepting drops.
  • Methods of DropTarget include some which deal
    with events (e.g., drop, dragOver), methods
    dealing with auto scrolling (e.g.,
    updateAutoScroll), methods to manage listeners
    (e.g., removeDropTargetListener) and others.

10
Drag and Drop cont.
  • DropTargetListener interface includes methods
    dragEnter, dragExit, dragOver, drop and
    dropActionChanged.
  • DnDConstants include ACTION_COPY, ACTION_MOVE,
    ACTION_LINK and ACTION_NONE. 
  • Class DropTargetDropEvent is delivered when an
    object is dropped.  It reports the source drop
    actions (DnDConstant, drop options supported by
    source) and the user drop action (depends on
    modifier keys ctrlshift link, ctrl copy,
    shift move).  User drop action will be
    ACTION_NONE if the selected option is not
    supported by the drag source.
  • The getTransferable method of DropTargetDropEvent
    can be used to access the Transferable object
    associated with the drop.
  • Class DataFlavor represents the data format used
    during drag and drop, in the clipboard, or in a
    file system.  It provides static constants that
    can be used for comparison to common formats.

11
Drag and Drop
Source
Target
JEditorPane (our program - WebBrowserPane)
html doc rtf doc text doc
setDropTarget
associates handler
DropTargetHandler (implements DropTargetListener)
dragEnter dragExit dragOver drop dropActionChanged
drop get Transferable from event check
DataFlavor (image, fileList, string, MIME
etc.) event.acceptDrop process (we
goToURL) event.dropComplete indicates success
12
Internationalization
  • Process of preparing an application for
    distribution in multiple locales
  • A locale identifies the language, currency,
    character set, date formats etc. for a particular
    country or region.
  • To internationalize, replace hard-coded strings
    such as labels, tooltips etc. with strings
    contained in a ResourceBundle.
  • The ResourceBundle class includes a static method
    getBundle which is used to load application
    specific strings.
  • The getString method of ResourceBundle is used to
    retrieve strings.  The parameter to getString
    should match one of the strings listed in the
    resource file.
  • NumberFormat and DateFormat classes can use
    information about the locale when producing
    output.  For example, the format command of
    DateFormat can take both a Date and Locale as
    arguments.
  • Internationalized applications must use Unicode,
    the 16-bit character code that includes symbols
    from many different languages (more than can be
    represented in the 8-bit ASCII code).  Java
    always uses Unicode, but data generated by other
    applications might not be. 

13
Format of the resource file
  • Files must be named properly to be associated
    with various locales.
  • The filename for the default Locale is ltfile name
    matching parameter to getBundlegt.properties
  • Ex StringsAndLabels.properties
  • For other locales, the name is ltfile name
    matching parameter_languageCode_countryCodegt.prope
    rties
  • ExStringsAndLabels_fr_FR.properties.
  • Escape characters in the resource file should be
    Unicode escape sequences, which are \u followed
    by four hexadecimal digits. 
  • ISO language codes are available at
    www.ics.uci.edu/pub/ietf/http/related/iso639.txt
  • ISO country codes are available at
  • www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html

14
Example
  • Properties for English English language strings
    for internationalized WebBrowser application
    titleapplicationTitle Deitel Web Browser
    title for WebToolBartoolBarTitle Web
    Navigation tooltip for forward toolbar
    buttonforwardToolTip Next Page tooltip for
    back buttonbackToolTip Previous Page

15
Example cont.
  • Properties for French
  • French language strings for internationalized
    WebBrowser tooltip for back buttonbackToolTip
    Page pr\u00E9c\u00E9dente application
    titleapplicationTitle Logiciel de Navigation
    de Deitel title for WebToolBartoolBarTitle
    Navigation des Pages sur la Toile tooltip for
    forward toolbar buttonforwardToolTip Prochaine
    Page

16
Obtaining a list of Locales
  • import java.text.DateFormat
  • import java.util.Locale
  • public class Locales
  • public static void main(String args)
  • // Locale class contains the default location
  • System.out.println("Default locale is "
    Locale.getDefault())
  • // Date Format class maintains list of all
    locales
  • Locale locales DateFormat.getAvailableLoca
    les()
  • String spaces " "
  • for (int i 0 i lt locales.length i)
  • if (localesi.getCountry() ! "") // omit
    locales with only language
  • // add spaces to keep columns aligned
  • System.out.println(localesi
    spaces.substring(localesi.toString().length())
  • localesi.getDisplayName())

17
Seeing Number Formats
  • / Formats numbers, currency, and percents, first
    in the
  • default locale, then using a language and
    country passed
  • as program arguments.
  • /
  • import java.text.NumberFormat
  • import java.util.Locale
  • public class Numbers
  • public static void main(String args)
  • NumberFormat numDefault NumberFormat.getNumb
    erInstance()
  • NumberFormat curDefault NumberFormat.getCurr
    encyInstance()
  • NumberFormat perDefault NumberFormat.getPerc
    entInstance()
  • System.out.println("Max fraction digits "
    numDefault.getMaximumFractionDigits())
  • double d 12345.67899
  • System.out.println(numDefault.format(d))
  • System.out.println(curDefault.format(d))
  • System.out.println("Min fraction digits "
    numDefault.getMinimumFractionDigits())
  • System.out.println(perDefault.format(d))

18
Seeing Date Formats
  • import java.text.DateFormat
  • import java.util.Locale
  • import java.util.Calendar
  • import java.util.GregorianCalendar
  • import java.util.Date
  • public class DateTime
  • public static void main(String args)
  • Calendar calendar new GregorianCalendar(2000
    ,6,4,14,10,5)
  • Date theFourth calendar.getTime()
  • DateFormat fullDefault DateFormat.getDateTim
    eInstance
  • (DateFormat.FULL,DateFormat.FULL) // style for
    date and time
  • DateFormat longDefault DateFormat.getDateTim
    eInstance (DateFormat.LONG,DateFormat.LONG)
  • DateFormat mediumDefault DateFormat.getDateT
    imeInstance (DateFormat.MEDIUM,DateFormat.MEDIUM)
  • DateFormat shortDefault DateFormat.getDateTi
    meInstance (DateFormat.SHORT,DateFormat.SHORT)
  • DateFormat justDate DateFormat.getDateInstan
    ce()
  • DateFormat justTime DateFormat.getTimeInstan
    ce()
  • System.out.println(fullDefault.format(theFourt
    h))
  • System.out.println(longDefault.format(theFourt
    h))

19
MessageFormat
  • MessageFormat provides a means to produce
    concatenated messages in language-neutral way.
  • Ex The date is Tuesday, July 4, 2000
  • First part (The date is ) is constant.
  • Actual date format will vary with location.
  • String s The date is 0
  • String formatted MessageFormat.format(s,
    arguments)

20
Chapter 2 Exercises
  • Read/execute web browsers. Next homework will
    involve web browsing.
  • Add another action to ActionSample. Be sure to
    update toolbar, menu and add a button.
  • Add another country to BrowserLauncher.
Write a Comment
User Comments (0)
About PowerShow.com