Java Reference
In-Depth Information
Note
AWTprovidesthe
java.awt.Insets
classtospecifytheamountofspace
thatacontainerleavesemptyatitsedges.Forexample,
Frame
hasatopinsetthatcor-
respondstotheheightoftheframewindow'stitlebar.Bordersextendtheinsetsconcept
byletting youselect anobject that drawsoverthis empty space. Borders leverage in-
sets. For example,
Border
declares
Insets getBorderInsets(Component
c)
to return the insets for the specified container component.
UI Delegates
Inthelate1970s,XeroxPARCinventedtheModel-View-Controller(MVC)architecture
as an architectural pattern for separating application logic from the user interface, to
simplify GUI creation.
MVC consists of the following entities:
• The
model
maintainsacomponent'sstate,suchasabutton'spressinformation
or the characters that appear in a textfield.
• The
view
presentsavisualrepresentationofthemodel,givingacomponentits
look
.Forexample,abuttonviewwouldtypicallydisplayabuttonaspressedor
unpressed according to its model's pressed state.
• The
controller
determines how (and even if) a component responds to input
events that originate from input devices (such as mice and keyboards), giving
the component its
feel
. For example, when the user presses a button, the con-
trollernotifiesthemodeltoupdateitspressedstateandtheviewtorepaintthe
button.
Experiencehasshownthatit'seasiertomanageanintegratedviewandcontrollerthan
todealwiththemseparately.Theintegratedresultisknownasa
User Interface (UI) del-
egate
.
Swing components are based on models and UI delegates, where the UI delegate
makesitpossibleforacomponenttolookthesamenomatterwhatwindowingsystem
underliestheGUI.ModelsandUIdelegatesareseparateandcommunicate viaevents,
makingitpossibleforaUIdelegatetoassociatewithmultiplemodelsandforamodel
to associate with multiple UI delegates.
ASwingcomponentconsistsofamainclasswhosenamestartswithJ,acurrentmod-
el,andacurrentUIdelegate.ThemainclassconnectsthemodeltotheUIdelegateand
is used to create the component.
Forexample,the
JButton
classdescribesabuttoncomponent.It'sassociatedwitha
modelthat'sdescribedbythe
javax.swing.ButtonModel
interface.Themodelis