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

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);
}
select() with zero as the index.

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
}
changeColor() method, which is described below (see
Making the color change.) A predetermined color is passed as an argument, along with its correct amounts of red, green, and blue.
switch() statement on the getSelectedIndex() of the list box to determine the appropriate color to use.
Color.black and Color.blue.

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());
}
}
changeColor() with the new color information, which is described below.
deselect() on the currently selected index, obtained with getSelectedIndex().

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);
}
changeColor() is defined in the General section of the help system. It takes a parameters for the red, green and blue values.
setColor() or setForGround(), depending on the type of control.
setValue() with the new red, green, and blue values.