Java Reference
In-Depth Information
1. Whentheuserclicksthesearchbutton,its
actionPerformed()
methodis
invoked on the EDT.
2.
actionPerformed()
accesses the starting directory and search text text-
fields,clearstheresultstextareasothatnewsearchresultsarenotappendedto
previoussearchresults,createstherunnable,andstartsaworkerthread(thatex-
ecutes this runnable) on the EDT.
3. Shortlythereafter,theworkerthreadwillstarttoexecutetherunnablebyinvok-
ing its
run()
method.
4.
run()
invokes
findAll()
to begin the search. If
findAll()
returns
false, a new runnable is created that outputs an error message via a
javax.swing.JOptionPane
-based dialog box. The worker thread ex-
ecutes
java.awt.EventQueue
's
invokeLater()
methodtoensurethat
the dialog box is displayed on the EDT.
Note
Appendix C introduces the
javax.swing.SwingWorker
class, which
simplifies communicating between a worker thread and the EDT.
Listing 8-21
reveals the following code:
pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS));
This code uses Swing's
javax.swing.BoxLayout
class to layout a container's
components in a vertical column. Unlike
java.awt.GridLayout
,
BoxLayout
doesn't give each component the same size.
Because many search results may be returned, the textarea needs to be scrollable.
However,thiscomponentisn'tscrollablebydefault,soitmustbeaddedtoascrollpane.
This task is accomplished with the help of the
javax.swing.JScrollPane
class.
JScrollPane
providesconstructorsthatarecalledwiththecomponentthatneeds
to be made scrollable; for example,
JScrollPane(Component view)
. In con-
trast, AWT's
java.awt.ScrollPane
class requires you to pass the component to
its
add()
method.