Java Reference
In-Depth Information
16
import
javax.swing.JButton;
17
import
javax.swing.Box;
18
import
javax.swing.JLabel;
19
import
javax.swing.JTextField;
20
21
22
23
24
import
javax.swing.RowFilter;
import
javax.swing.table.TableRowSorter;
import
javax.swing.table.TableModel;
public class
DisplayQueryResults
extends
JFrame
25
{
26
// database URL, username and password
27
private static final
String
DATABASE_URL
=
"jdbc:derby:books"
;
28
private static final
String
USERNAME
=
"deitel"
;
29
private static final
String
PASSWORD
=
"deitel"
;
30
31
// default query retrieves all data from Authors table
32
private static final
String
DEFAULT_QUERY
=
"SELECT * FROM Authors"
;
33
34
private
static
ResultSetTableModel tableModel;
35
36
public static void
main(String args[])
37
{
38
// create ResultSetTableModel and display database table
39
try
40
{
41
// create TableModel for results of query SELECT * FROM Authors
tableModel =
new
ResultSetTableModel(
DATABASE_URL
,
USERNAME
,
PASSWORD
,
DEFAULT_QUERY
);
42
43
44
45
// set up JTextArea in which user types queries
46
final
JTextArea queryArea =
new
JTextArea(
DEFAULT_QUERY
,
3
,
100
);
47
queryArea.setWrapStyleWord(
true
);
48
queryArea.setLineWrap(
true
);
49
50
JScrollPane scrollPane =
new
JScrollPane(queryArea,
51
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED
,
52
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
);
53
54
// set up JButton for submitting queries
55
JButton submitButton =
new
JButton(
"Submit Query")
;
56
57
// create Box to manage placement of queryArea and
58
// submitButton in GUI
59
Box boxNorth = Box.createHorizontalBox();
60
boxNorth.add(scrollPane);
61
boxNorth.add(submitButton);
62
63
// create JTable based on the tableModel
JTable resultTable =
new
JTable(tableModel);
64
65
66
JLabel filterLabel =
new
JLabel(
"Filter:"
);
67
final
JTextField filterText =
new
JTextField();
68
JButton filterButton =
new
JButton(
"Apply Filter"
);
Fig. 24.28
|
Display the contents of the
Authors
table in the
books
database. (Part 2 of 5.)