Tutorial One: Part Three
Adding a Set Format panel
In this final section we will add the option of setting the time format.
Adding a panel and setting the layout:
- Switch back to the GUI Editing Workspace to see your editing windows - the Explorer, Form Editor window, Component Inspector, and Editor.
- Add a new JPanel to the East panel of the Form Editor window surface. Select the new JPanel item in the Component Inspector, and flip to its Layout tab. You will see the Direction property is set to East, where you just placed the JPanel. In fact we want this new panel on the South part of the Form - click the direction, and select South from the drop-down list. You will see the JPanel repositioned in the new location.
- We will again change the Layout of this new JPanel - select GridLayout from the Layout tab of the Component Palette, and drop it onto the new JPanel. Select the GridLayout in the Component Inspector, and change the
Rows
property from the default of 2 to 1.Add some components:
- Position a JLabel from the Swing tab of the Component Palette on the new JPanel. Also add a JTextField, and lastly, a JButton. The components will appear in the order you place them, across the South panel of the Form.
Setting the properties:
- Again we will modify the default properties of these new components. Set the JLabel's
variable name
to jlblNewFormat, and itstext
property to Time Format. Change itshorizontalAlignment
property toCENTER
.- Set the JTextField
variable name
to jtfNewTimeFormat, and change thedefault text
to hh:mm:ss.- Set the JButton
variable name
to jbtnNewTimeFormat. Set thetext
to read Set new time format.
Adding functionality:
- Select the
jbtnNewTimeFormat
button in the Component Inspector, and flip to its Events panel. Set theactionPerformed
event to jbtnNewTimeFormatClicked. You will see the new event handler generated in the code.- Add the following to the handler generated:
String timeFormat = jtfNewTimeFormat.getText(); formatter = new SimpleDateFormat(timeFormat);Compiling and executing:
You can now set the time format using the SimpleDateFormat syntax (described in the JDK documentation -
$JDK_HOME
/docs/api/java/text/SimpleDateFormat.html
, where $JDK_HOME is the directory where the JDK installed)For example, try the entering following in your
New Format
text panel:EEEE, d MMMM, hh:mm:ss a .
This concludes Tutorial One. On the Running Workspace, right-click on the ClockFrame item appearing in the Execution Window, and select Terminate Process. This will close the currently running ClockFrame.
In Tutorial Two: Part Two, we will use Forte for Java's beans support to build a JavaBeans component, and then we will build a form using that bean to set the background color using RGB sliders.
Beginning | Prev | Next |