Swing Interfaces with Sorting and Searching - PowerPoint PPT Presentation

1 / 49
About This Presentation
Title:

Swing Interfaces with Sorting and Searching

Description:

Three parallel arrays, one each for the title, studio, and year ... Write a method to return the Container object which acts as a content pane ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 50
Provided by: steve1752
Category:

less

Transcript and Presenter's Notes

Title: Swing Interfaces with Sorting and Searching


1
Chapter 7
  • Swing Interfaces with Sorting
    and Searching

2
Chapter Objectives
  • List features of the Java Foundation Classes
    (JFC)
  • Differentiate between AWT and Swing components
  • Create a JFrame application
  • Sort data in parallel arrays

3
Chapter Objectives
  • Use the keyword, super, in a constructor method
  • Create a tool tip
  • Use methods associated with JPanels, JComboBoxes,
    JLabels, JTextPanes, and JScrollPanes
  • Use Tabs and Styles in a JTextPane

4
Chapter Objectives
  • Use methods associated with the Document class
  • Perform linear searches
  • Incorporate Look and Feel methods in an interface

5
Introduction
  • Swing components are GUI components
  • Provides a wide range of objects and methods to
    build user interfaces
  • Includes lightweight Java-based components that
    allow programmers to tailor windows
  • The application in this chapter stores a DVD
    collection of classic movies
  • GUI and menu system use Swing components
  • A formatted list is displayed with scrolling
    capabilities
  • The program allows insertions, as well as sorts
    and searches of the list by title, studio, or year

6
Classics on DVD Application
7
(No Transcript)
8
Problem Analysis
  • Insert
  • Users can enter a new movie title, studio, and
    year
  • Search
  • User enter a search argument for a specific field
    and the program finds and displays the results
  • Sort
  • Users request a alphanumeric sort of all data
    based upon a certain field
  • The program reorders all accompanying data based
    on that field

9
Design the Solution
  • Scrollable text area to display a formatted movie
    list
  • Drop-down list for sort choices
  • Menu options with shortcut keys underlined
  • A shortcut key is a special key combination used
    to invoke commands on a menu
  • Provide users with different samples of formatted
    text
  • Font family, italic, bold, and large attributes

10
(No Transcript)
11
Menu Storyboard
12
Program Design
  • Data storage
  • Since this program is a prototype, data can be
    stored during program execution instead of in a
    database
  • Three parallel arrays, one each for the title,
    studio, and year
  • Subscript is the same for each piece of movie
    data, forming a record across the arrays
  • Searching
  • Linear search looks through the list sequentially
  • Sorting
  • Users specifies a field, the corresponding array
    is sorted, and the other arrays are modified
    accordingly

13
Sorting parallel arrays
14
Swing and Java Foundation Classes
15
Swing versus AWT
  • Swing components are not implemented with native
    code and have more functionality than AWT
    components

16
(No Transcript)
17
Swing Container Hierarchy
  • Containers play a more important role in Swing
    than in AWT
  • Swing components should be placed in a top-level
    Swing container
  • First, use a content pane to hold the components,
    and then add the content pane to the top-level
    container
  • The getContentPane() method creates a Container
    instance
  • Avoid combining complex AWT components in the
    same container as Swing components
  • When Swing and AWT components overlap, the
    heavyweight component is painted on top

18
(No Transcript)
19
Extending JFrame
  • JFrame is a top-level container
  • Programs implementing ActionListener must include
    an actionPerformed() method before they will
    compile successfully

20
Class-Level Components
  • Visible to all other methods within the class
  • JLabel can display labels, borders, and images
  • JComboBox is a drop-down list and can be editable
  • The default is uneditable
  • JTextPane is a text component that displays with
    graphic attributes
  • Each paragraph has a logical style with default
    attributes
  • Three String arrays

21
(No Transcript)
22
Constructing Parallel Arrays
  • Populate the arrays as they are constructed
  • The array length is automatically set equal to
    the number of items in the constructor method
  • An alternative method would be to fill the arrays
    during execution by reading in the values from a
    database

23
Using the super keyword
  • When inheriting from another class, a constructor
    must call a constructor of its immediate
    superclass
  • A superclass default constructor can be called
    implicitly
  • An explicit call, if needed, is made using the
    keyword, super

24
Creating a JMenuBar
  • JMenuBar creates an object that can be set at the
    top of a JFrame through the setJMenuBar() method
  • JMenu objects are the commands on a JMenu bar
  • JMenuItems display on the drop-down list for each
    JMenu
  • Mnemonic keys can be set for a Menu or MenuItem
    object
  • A mnemonic key is the keyboard letter which, when
    combined with another key, activates the object
  • The mnemonic key is specified with a VK keycode
    from the KeyEvent class

25
(No Transcript)
26
Creating a Menu System
  • setMnemonic()
  • Sets the keyboard mnemonic for a Menu or MenuItem
    object
  • Object.setMnemonic(KeyEvent.keycode)
  • setDisplayedMnemonicIndex()
  • Assigns the shortcut key to a letter in the
    commands keyword, underlining the letter
  • setActionCommand()
  • Assigns a String that can be evaluated in the
    actionPerformed() method
  • addActionListener()
  • Causes the click of the menu command to trigger
    an event
  • add()
  • Adds its argument to the menu system

27
Creating the Content Pane
  • Write a method to return the Container object
    which acts as a content pane
  • Container is the superclass of all containers in
    the API
  • setToolTipText()
  • The tool tip displays when the mouse moves over
    the object
  • JScrollPane
  • Facilitates a scrollable view of JTextPane
  • The viewport is the visible part of the pane at
    any given time
  • Uses a Dimension object to set the preferred size
  • setVerticalScrollBarPolicy() uses a constant to
    set the scrollbar
  • getContentPane() creates an instance of the
    Container
  • Can be used with JApplets or JFrames

28
(No Transcript)
29
createContentPane()
30
Tabs and Styles
  • TabStop class
  • Creates predetermined tab positions
  • TabSet class
  • Comprised of TabStops and is used to locate a
    specific TabStop
  • StyleContext class
  • Contains a pool of styles which are reused by
    various style definitions
  • AttributeSet class
  • Uses a defined StyleContext to set attributes for
    an object
  • Style class
  • Stores a collection of attributes associated with
    an element in a document

31
(No Transcript)
32
setTabsAndStyles()
33
The Document Class
  • Creates a container for Swing text components
  • Useful for appending and refreshing text-based
    Swing components

34
addTextToTextPane() Method
35
The actionPerformed() Event
  • Use getActionCommand() to retrieve the String
    assigned to a component that can have multiple
    states
  • Use getSource() to retrieve the clicked object
  • If the combo box was clicked, the index number is
    compared in a switch statement to specify the
    sort
  • If Insert was clicked, enlargeArray() creates a
    new array to accommodate the new movie data
  • New data is sent to each array, which is then
    sorted
  • If Search was clicked, the field name and array
    are sent to the search() method

36
Coding the enlargeArray() Method
  • Accepts an array and returns a new array that is
    one element longer
  • The actionPerformed() method inserts the new data
    at the end of the new array

37
Sorting an Array
  • Bubblesort
  • When a pair of elements is examined, the
    interchanged element moves to the top of the
    array
  • Selection sort
  • The array is searched for its lowest-value
    element, which is then positioned at the
    beginning of the array
  • The process is repeated for each element, placing
    the elements in the next unsorted location
  • Insertion sort
  • Creates a new array and inserts the values in
    order
  • Merge sort
  • Takes two previously sorted arrays of the same
    data type and sorts them into one list

38
sort()
39
Searching an Array
  • Linear search
  • Looks sequentially through the array one element
    at a time
  • Necessary when data is not in order
  • Inefficient for large amounts of data
  • Binary search
  • Suited for large arrays of sorted data
  • The search data is first compared with the middle
    element
  • If search data is lower, the middle element
    become the new high
  • If search data is higher, the middle element
    becomes the new low
  • Searches continue to reduce the array by one-half
    until a match is found

40
Linear search
41
Binary search
42
search()
43
Look and Feel
  • Gives any program using Swing programs a choice
    of how windows, title bars, and components will
    appear
  • Java look and feel, Windows look and feel, etc.
  • The choices are called window decorations
  • Developers can design their own Look and Feel
    objects

44
The main() method
  • setDefaultLookandFeelDecorated() method
  • Construct an instance of the DVD object
  • setDefaultCloseOperation() controls the action of
    the close button
  • Wrap the create Menu and Pane methods inside the
    set Menu and Pane methods

45
Testing
  • Compile the program and fix any errors
  • Test the sorting of the different fields
  • Test the addition of a DVD title, studio, and
    year
  • Test the search capabilities with a specific
    title, studio, and year
  • Test the other menu commands and shortcut keys

46
Chapter Summary
  • List features of the Java Foundation Classes
    (JFC)
  • Differentiate between AWT and Swing components
  • Create a JFrame application
  • Sort data in parallel arrays

47
Chapter Summary
  • Use the keyword, super, in a constructor method
  • Create a tool tip
  • Use methods associated with JPanels, JComboBoxes,
    JLabels, JTextPanes, and JScrollPanes
  • Use Tabs and Styles in a JTextPane

48
Chapter Summary
  • Use methods associated with the Document class
  • Perform linear searches
  • Incorporate Look and Feel methods in an interface

49
Chapter 7 Complete
  • Swing Interfaces with Sorting
    and Searching
Write a Comment
User Comments (0)
About PowerShow.com