Java Reference
In-Depth Information
121
122
123
124
125
// create listener for filterButton
126
filterButton.addActionListener(
127
new
ActionListener()
128
{
129
// pass filter text to listener
130
public void
actionPerformed(ActionEvent e)
131
{
132
String text = filterText.getText();
133
134
if
(text.length() ==
0
)
135
136
else
137
{
138
try
139
{
140
141
142
}
143
catch
(PatternSyntaxException pse)
144
{
145
JOptionPane.showMessageDialog(
null
,
146
"Bad regex pattern"
,
"Bad regex pattern"
,
147
JOptionPane.ERROR_MESSAGE
);
148
}
149
}
150
}
151
}
152
);
153
// dispose of window when user quits application (this overrides
154
// the default of HIDE_ON_CLOSE)
155
window.setDefaultCloseOperation(
DISPOSE_ON_CLOSE
);
156
window.setSize(
500
,
250
);
157
window.setVisible(
true
);
158
159
// ensure database is closed when user quits application
160
addWindowListener(
161
new
WindowAdapter()
162
{
163
164
165
166
167
168
169
}
170
);
171
}
172
catch
(SQLException sqlException)
173
{
final
TableRowSorter<TableModel> sorter =
new
TableRowSorter<TableModel>(tableModel);
resultTable.setRowSorter(sorter);
sorter.setRowFilter(
null
);
sorter.setRowFilter(
RowFilter.regexFilter(text));
// disconnect from database and exit when window has closed
public void
windowClosed(WindowEvent event)
{
tableModel.disconnectFromDatabase();
System.exit(
0
);
}
Fig. 24.28
|
Display the contents of the
Authors
table in the
books
database. (Part 4 of 5.)