How to set fonts for form controls at runtime.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on coding Java for SilverStream applications in the Programmer's Guide |
This example sets a font programmatically. You select a font name, size, and style from three list boxes. A text field then displays some sample text in the chosen font.
The following code illustrates how to set up default choices in the list boxes.
protected void formLoaded() { // Select the Dialog font lbFont.setSelectedValue("Dialog", true); // Select the font size 16 lbSize.setSelectedValue(new Integer(16), true); // Select the font style Italic lbStyle.setSelectedValue(new Integer(ITALIC), true); }
setSelectedValue()
on each list box.
The following code illustrates how a custom method is called when you make a selection from the list box. The custom method will translate the choice into a new font name.
private void handle_lbFont_valueChanged(AgoPropertyChangeEvent evt) { // Get the source of the value change int source = evt.getChangeSource(); // Only change the font for user and program value changes if (source == AgoValueChangedInfo.SOURCE_USER || source == AgoValueChangedInfo.SOURCE_PROG) { changeFont(); } }
changeFont()
method, which is described below.
The following code illustrates how a custom method is called when you make a selection from the list box. The custom method will translate the choice into a new font size.
private void handle_lbSize_valueChanged(AgoPropertyChangeEvent evt) { // Get the source of the value change int source = evt.getChangeSource(); // Only change the font size for user and program value changes if (source == AgoValueChangedInfo.SOURCE_USER || source == AgoValueChangedInfo.SOURCE_PROG) { changeFont(); } }
changeFont()
method, which is described below.
The following code illustrates how a custom method is called when you make a selection from the list box. The custom method will translate the choice into a new font style.
private void handle_lbStyle_valueChanged(AgoPropertyChangeEvent evt) { // Get the source of the value change int source = evt.getChangeSource(); // Only change the font style for user and program // value changes if (source == AgoValueChangedInfo.SOURCE_USER || source == AgoValueChangedInfo.SOURCE_PROG) { changeFont(); } }
changeFont()
method, which is described below.
The following code illustrates how to change the font of a sample text field to reflect the font name, size, and style currently selected in the list boxes. This is a custom (user-defined) method that you can view in the General section of the Programming Editor.
public void changeFont() { // Get the index of the selected font int iSelectedRow = lbFont.getSelectedIndex(); if (iSelectedRow < 0) return; // No font selected, so return // Get the index of the selected font size iSelectedRow = lbSize.getSelectedIndex(); if (iSelectedRow < 0) return; // No font size selected, so return // Get the index of the selected font style iSelectedRow = lbStyle.getSelectedIndex(); if (iSelectedRow < 0) return; // No font style selected, so return // Get the selected font size int iSize = Integer.parseInt((String)lbSize.getValue()); // Get the selected font style int iStyle = Integer.parseInt((String)lbStyle.getValue()); // Get the font name String sName = lbFont.getItemValue( lbFont.getSelectedIndex()).toString(); Font newFont = new Font(sName, iStyle, iSize); // Change the label's font attributes to those of the new // font identifier lblExample.setFont(newFont); }
getSelectedIndex()
for each list box.
getValue()
with Integer.parseInt()
to get the associated size and style values as defined in the Property Inspector for those list boxes. These values will be used later in the method.
getIemValue()
on getSelectedIndex()
of the font name list box. (The index must be converted to a String)
setFont()
.