Application Techniques



Setting Fonts

How to set fonts for form controls at runtime.

About this technique

Details

Category

Core Programming Techniques> Java

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

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.

Setting a default font name, size, and style in list boxes   Top of page

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); 
  } 

Notes about the code

Handling a user selection from the font name list box   Top of page

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(); 
        } 
    } 

Notes about the code

Handling a user selection from the font size list box   Top of page

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(); 
       } 
    } 

Notes about the code

Handling a user selection from the font style list box   Top of page

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(); 
       } 
    } 

Notes about the code

Making the font change   Top of page

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); 
    } 

Notes about the code






Copyright © 2000, SilverStream Software, Inc. All rights reserved.