Java Reference
In-Depth Information
the Properties Inspector; or, select it from your project by clicking the
Browse
button. If you type the name of a class that doesn't yet exist,
IDEA
will ask to cre-
ate it for you by offering an intention action (described in detail in chapter 3).
Although there is no requirement around the naming of forms and the
naming of their underlying classes—they can be vastly different, which is
perfectly legitimate—the convention of giving them the same name (in
the same package) can help eliminate confusion. You'll always remem-
ber that the functionality for
MainGUI.form
is found in
MainGUI.java
.
TIP
Binding individual components to field variables
In order to access the buttons, form fields, and other components in your form,
you must bind each of them to a member variable of your form class. If you don't
need to access a particular component (for example, if you never have to manipu-
late a label), there is no need to bind it.
To bind a component, select it in the
GUI
Designer, and click the
binding
entry
in the Properties Inspector. Now you may either select an existing member vari-
able from the list (the list is filtered to include only the appropriate types of field
variables) or type in the name of field variable you'd like to use and let
IDEA
's
intention actions create it for you.
When we're using the
GUI
Designer, we rarely create fields ahead of
time. As soon as we realize we need to programmatically access a compo-
nent, we edit the
bind
property of the component, type in an appropri-
ate variable name, and let
IDEA
take over.
TIP
To display your form, you need to bind your top-level panel to a field in your
form class. This panel is the panel you'll use to display your
GUI
.
10.5.2
Creating a constructor
You need a constructor (or you can let Java create an empty one for you if none
is explicitly defined) for your form class so that it may be instantiated. You can
initialize the fields of your form class either in the constructor or in their decla-
ration. At compile time,
IDEA
automatically (and transparently) adds code to the
constructor that will initialize the form and bind it to your class's field variables.
This will happen ahead of any additional code you place into the constructor,
allowing you to further customize the components within it.