Hello GUI World

A.K.A. Using GUIs in Introductory Java by Letting the IDE (NetBeans 6) Do the Complicated Things

1.       Create a new Project

·         File > New Project

·         Select Category = Java    then Select  Projects = Java Application

·         Select Next button

·         Specify a project name

·         Browse to a project location

·         Make sure that “Create Main Class” is NOT checked

·         Select Finish button

2.        Create a class for the GUI

·         In the projects pane, right click on the project

·         Select  New > JFrame Form …

·         Specify a class name and hopefully a package name

·         Select Finish button

3.        Put components on the form. This can be a little less than ideal without getting involved in explaining Layout Managers and JPanels.

·         From Palette pane, drag components onto form (including at least one jbutton)

·         In the Properties pane, change any properties you’d like to change (e.g. especially Text, Font, Foreground Color  (for background color of some controls to take effect you have to set the opaque property to true (checked)))

4.       (as in Visual Studio, I would strongly recommend and model for the students changing controls (components’) names.)

·         Right-click on the control or on the name of the control in the Inspector pane

·         Select “Change Variable Name”

·         Specify the new, friendly name

5.        Add code to handle the event

·         Right-click on the control or on the name of the control in the Inspector pane

·         Select Events>Action>ActionPerformed >   (for a jbutton, or otherwise select  other relevant event)

·         Enter code (see below). Do not change any generated code (in blue background)

6.        Entering Code

·         To get contents of a jTextField, use jtextfieldname.getText();

·         To set the contents of a jTextField or jLabel, use obj.setText(  text);

·         All control text properties are String valued. To convert use parse methods.

·   E.g.  double width = Double.parseDouble(txtWidth.getText());

7.        The IDE still needs to know where to look for the main class –

·         Set the project’s Main Class property (under run category) OR

·         When running, choose the main class

·         Right click on the file and choose Run File