Java Reference
In-Depth Information
69
Box boxSouth = Box.createHorizontalBox();
70
71
boxSouth.add(filterLabel);
72
boxSouth.add(filterText);
73
boxSouth.add(filterButton);
74
75
// place GUI components on JFrame's content pane
76
JFrame window =
new
JFrame(
"Displaying Query Results"
);
77
add(boxNorth,
BorderLayout.NORTH
);
78
add(
new
JScrollPane(resultTable),
BorderLayout.CENTER
);
79
add(boxSouth,
BorderLayout.SOUTH
);
80
81
// create event listener for submitButton
82
submitButton.addActionListener(
83
new
ActionListener()
84
{
85
public void
actionPerformed(ActionEvent event)
86
{
87
// perform a new query
88
try
89
{
90
91
}
92
catch
(SQLException sqlException)
93
{
94
JOptionPane.showMessageDialog(
null
,
95
sqlException.getMessage(),
"Database error"
,
96
JOptionPane.ERROR_MESSAGE
);
97
98
// try to recover from invalid user query
99
// by executing default query
100
try
101
{
102
103
queryArea.setText(
DEFAULT_QUERY
);
104
}
105
catch
(SQLException sqlException2)
106
{
107
JOptionPane.showMessageDialog(
null
,
108
sqlException2.getMessage(),
"Database error"
,
109
JOptionPane.ERROR_MESSAGE
);
110
111
112
113
114
System.exit(
1
);
// terminate application
115
}
116
}
117
}
118
}
119
);
120
tableModel.setQuery(queryArea.getText());
tableModel.setQuery(
DEFAULT_QUERY
);
// ensure database connection is closed
tableModel.disconnectFromDatabase();
Fig. 24.28
|
Display the contents of the
Authors
table in the
books
database. (Part 3 of 5.)