Application Techniques


Setting Colors

How to set colors 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

The colors of various form controls can be changed dynamically. This example provides code to change the color of a rectangle, a label, a text field, and a button. The user can select an entry from a list box to change the color. Alternatively, the user can customize the current color by dragging three sliders which control the amount of blue, green, and red. Numbers above the sliders indicate how much blue, green, and red are in the new color.

 
Top of page

Setting a default color choice in a list box

The following code illustrates how to set up a default choice in the list box.

  protected void formActivate()
    {
      // Display the text field's name
      fldExample.setText("Example Text");
      
      // Select the first color in the list box
      lbColors.select(0);
    }

Notes about the code

 
Top of page

Handling a user selection from the list box

The following code illustrates how to translate a selection made by the user from the list box into a color choice.

  private void handle_lbColors_valueChanged(AgoPropertyChangeEvent evt)
  {
  
      int iSource = evt.getChangeSource();
      int iSelection = 0;
  
      // Only change color for user or program color changes
      if (iSource == AgoValueChangedInfo.SOURCE_USER || iSource ==
          AgoValueChangedInfo.SOURCE_PROG)
      {
        // Call the changeColor method with the color settings
        // indicated by the current selection in the 
        // lbColors list box
        iSelection = lbColors.getSelectedIndex();
        if (iSelection == -1) return;       
        switch (iSelection)
        {
          case 0:
          {
            // Black
            changeColor(Color.black);
            break;
          }
          case 1:
          {
            // Blue
            changeColor(Color.blue);
            break;
          }
          case 2:
          {
            // Cyan
            changeColor(Color.cyan);
            break;
          }
          case 3:
          {
            // Dark gray
            changeColor(Color.darkGray);
            break;
          }
          case 4:
          {
            // Gray
            changeColor(Color.gray);
            break;
          }
          case 5:
          {
            // Green
            changeColor(Color.green);
            break;
          }
          case 6:
          {
            // Light gray
            changeColor(Color.lightGray);
            break;
          }
          case 7:
          {
            // Magenta
            changeColor(Color.magenta);
            break;
          }
          case 8:
          {
            // Orange
            changeColor(Color.orange);
            break;
          }
          case 9:
          {
            // Pink
            changeColor(Color.pink);
            break;
          }
          case 10:
          {
            // Red
            changeColor(Color.red);
            break;
          }
          case 11:
          {
            // White
            changeColor(Color.white);
            break;
          }
          case 12:
          {
            // Yellow
            changeColor(Color.yellow);
            break;
          } 
        } // switch      
        //The change color method moves the three color sliders and 
        //causes the list box to lose focus. 
        lbColors.select(iSelection);      
      }  // if evt source valid      
  }

Notes about the code

 
Top of page

Handling red, green, and blue sliders

The following code shows how the slider controls are handled when the user changes their values are changed.

NOTE   The code in this example is for the slider which controls the amount of blue. The code for the red and green sliders is similar to this, except that the label text fields lblGreenValue and lblRedValue are updated instead of lblBlueValue.

  private void handle_sldBlue_valueChanged(AgoPropertyChangeEvent evt)
  {
      // Only change color for user color changes
      if (evt.getChangeSource() == AgoValueChangedInfo.SOURCE_USER)
      {
        // Get the current red RGB value
        int iRed = sldRed.getValue();
        
        // Get the current green RGB value
        int iGreen = sldGreen.getValue();
        
        // Get the new blue RGB value
        int iBlue = sldBlue.getValue();
        
        // Update the label for the blue slider with the new 
        // blue RGB value
        lblBlueValue.setText("" + iBlue);
        
        // Call the changeColor method
        changeColor(iRed, iGreen, iBlue);
        
        // As the user has changed the color using the blue slider,
       // we must de-select the currently selected color in 
       // the list box
        lbColors.deselect(lbColors.getSelectedIndex());
      }
  }

Notes about the code

 
Top of page

Making the color change

The following code illustrates a custom method which actually changes the color of certain controls. This method is called whenever the user makes a selection from the list box or moves a slider.

  public void changeColor(int piRed, int piGreen, int piBlue)
   {
      // Create a new color object with the current RGB settings
      Color colExample = new Color(piRed, piGreen, piBlue);
  
      // Set the rectangle's color
      rctExample.setColor(colExample);
    
      // Set the label's color
      lblExample.setForeground(colExample);
    
      // Set the text field's color
      fldExample.setForeground(colExample);
    
      // Set the text button's color
      btnExample.setForeground(colExample);
      
      // Set the red slider and its label to the new red RGB value
      sldRed.setValue(piRed);
      lblRedValue.setText( "" + piRed);
      
      // Set the green slider and its label to the new 
      // green RGB value
      sldGreen.setValue(piGreen);
      lblGreenValue.setText( "" + piGreen);
      
      // Set the blue slider to the new blue RGB value
      sldBlue.setValue(piBlue);
      lblBlueValue.setText( "" + piBlue);
    }

Notes about the code


Application Techniques

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