Java Programming, 3e
Concepts and Techniques
Chapter 7
Swing Interfaces
with Sorting
and Searching
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
Chapter 7: Swing Interfaces with Sorting and Searching 2
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
Chapter 7: Swing Interfaces with Sorting and Searching 3
Chapter Objectives
• Use methods associated with the
Document class
• Perform linear searches
• Incorporate Look and Feel methods in an
interface
Chapter 7: Swing Interfaces with Sorting and Searching 4
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
Chapter 7: Swing Interfaces with Sorting and Searching 5
Classics on DVD Application
Chapter 7: Swing Interfaces with Sorting and Searching 6
Chapter 7: Swing Interfaces with Sorting and Searching 7
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
Chapter 7: Swing Interfaces with Sorting and Searching 8
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
Chapter 7: Swing Interfaces with Sorting and Searching 9
Chapter 7: Swing Interfaces with Sorting and Searching 10
Menu Storyboard
Chapter 7: Swing Interfaces with Sorting and Searching 11
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
Chapter 7: Swing Interfaces with Sorting and Searching 12
Sorting parallel arrays
Chapter 7: Swing Interfaces with Sorting and Searching 13
Swing and Java Foundation
Classes
Chapter 7: Swing Interfaces with Sorting and Searching 14
Swing versus AWT
• Swing components are not implemented with native
code and have more functionality than AWT components
Chapter 7: Swing Interfaces with Sorting and Searching 15
Chapter 7: Swing Interfaces with Sorting and Searching 16
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
Chapter 7: Swing Interfaces with Sorting and Searching 17
Chapter 7: Swing Interfaces with Sorting and Searching 18
Extending JFrame
• JFrame is a top-level container
• Programs implementing ActionListener must include an
actionPerformed() method before they will compile
successfully
Chapter 7: Swing Interfaces with Sorting and Searching 19
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
Chapter 7: Swing Interfaces with Sorting and Searching 20
Chapter 7: Swing Interfaces with Sorting and Searching 21
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
Chapter 7: Swing Interfaces with Sorting and Searching 22
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
Chapter 7: Swing Interfaces with Sorting and Searching 23
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
Chapter 7: Swing Interfaces with Sorting and Searching 24
Chapter 7: Swing Interfaces with Sorting and Searching 25
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 command’s 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
Chapter 7: Swing Interfaces with Sorting and Searching 26
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
Chapter 7: Swing Interfaces with Sorting and Searching 27
Chapter 7: Swing Interfaces with Sorting and Searching 28
createContentPane()
Chapter 7: Swing Interfaces with Sorting and Searching 29
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
Chapter 7: Swing Interfaces with Sorting and Searching 30
Chapter 7: Swing Interfaces with Sorting and Searching 31
setTabsAndStyles()
Chapter 7: Swing Interfaces with Sorting and Searching 32
The Document Class
• Creates a container for Swing text components
• Useful for appending and refreshing text-based Swing
components
Chapter 7: Swing Interfaces with Sorting and Searching 33
addTextToTextPane() Method
Chapter 7: Swing Interfaces with Sorting and Searching 34
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
Chapter 7: Swing Interfaces with Sorting and Searching 35
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
Chapter 7: Swing Interfaces with Sorting and Searching 36
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
Chapter 7: Swing Interfaces with Sorting and Searching 37
sort()
Chapter 7: Swing Interfaces with Sorting and Searching 38
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
Chapter 7: Swing Interfaces with Sorting and Searching 39
Linear search
Chapter 7: Swing Interfaces with Sorting and Searching 40
Binary search
Chapter 7: Swing Interfaces with Sorting and Searching 41
search()
Chapter 7: Swing Interfaces with Sorting and Searching 42
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
Chapter 7: Swing Interfaces with Sorting and Searching 43
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
Chapter 7: Swing Interfaces with Sorting and Searching 44
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
Chapter 7: Swing Interfaces with Sorting and Searching 45
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
Chapter 7: Swing Interfaces with Sorting and Searching 46
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
Chapter 7: Swing Interfaces with Sorting and Searching 47
Chapter Summary
• Use methods associated with the
Document class
• Perform linear searches
• Incorporate Look and Feel methods in an
interface
Chapter 7: Swing Interfaces with Sorting and Searching 48
Java Programming, 3e
Concepts and Techniques
Chapter 7 Complete
Swing Interfaces
with Sorting
and Searching