Java Reference
In-Depth Information
ResultSetTableModel object and the GUI for the application and displays the GUI in a
JFrame . Line 64 creates the JTable object and passes a ResultSetTableModel object (cre-
ated at lines 42-43) to the JTable constructor, which then registers the JTable as a listener
for TableModelEvent s generated by the ResultSetTableModel .
The local variables queryArea (line 46), filterText (line 67) and sorter (lines 121-
122) are declared final because they're used from anonymous inner classes. Recall that
any local variable that will be used in an anonymous inner class must be declared final ;
otherwise, a compilation error occurs. (In Java SE 8, this program would compile without
declaring these variables final because these variables would be effectively final , as dis-
cussed in Chapter 17.)
Lines 82-119 register an event handler for the submitButton that the user clicks to
submit a query to the database. When the user clicks the button, method actionPer-
formed (lines 85-117) invokes method setQuery from the class ResultSetTableModel to
execute the new query (line 90). If the user's query fails (e.g., because of a syntax error in
the user's input), lines 102-103 execute the default query. If the default query also fails,
there could be a more serious error, so line 112 ensures that the database connection is
closed and line 114 exits the program. The screen captures in Fig. 24.28 show the results
of two queries. The first screen capture shows the default query that retrieves all the data
from table Authors of database books . The second screen capture shows a query that
selects each author's first name and last name from the Authors table and combines that
information with the title and edition number from the Titles table. Try entering your
own queries in the text area and clicking the Submit Query button to execute the query.
Lines 161-170 register a WindowListener for the windowClosed event, which occurs
when the user closes the window. Since WindowListener s can handle several window
events, we extend class WindowAdapter and override only the windowClosed event handler.
Sorting Rows in a JTable
JTable s allow users to sort rows by the data in a specific column. Lines 121-122 use the
TableRowSorter class (from package javax.swing.table ) to create an object that uses our
ResultSetTableModel to sort rows in the JTable that displays query results. When the
user clicks the title of a particular JTable column, the TableRowSorter interacts with the
underlying TableModel to reorder the rows based on the data in that column. Line 123
uses JTable method setRowSorter to specify the TableRowSorter for resultTable .
Filtering Rows in a JTable
JTable s can now show subsets of the data from the underlying TableModel . This is known
as filtering the data. Lines 126-152 register an event handler for the filterButton that
the user clicks to filter the data. In method actionPerformed (lines 130-150), line 132
obtains the filter text. If the user did not specify filter text, line 135 uses JTable method
setRowFilter to remove any prior filter by setting the filter to null . Otherwise, lines 140-
141 use setRowFilter to specify a RowFilter (from package javax.swing ) based on the
user's input. Class RowFilter provides several methods for creating filters. The static
method regexFilter receives a String containing a regular expression pattern as its argu-
ment and an optional set of indices that specify which columns to filter. If no indices are
specified, then all the columns are searched. In this example, the regular expression pattern
is the text the user typed. Once the filter is set, the data displayed in the JTable is updated
based on the filtered TableModel .
 
Search WWH ::




Custom Search