Microsoft SDK for Java

Using Dynamic Styles

A Style object can be thought of as a freestanding collection of properties. The term style is borrowed from the word processing model where the editing of a style sheet is independent of the documents to which you apply it. The same is true for using and applying Style objects in this library.

For example, to change the color of elements in your HTML pages to red, set the properties directly on their elements, which is the traditional model for GUI framework programming. This is shown in the following code.

       // Set the properties directly on their elements.
       DhText t1 = new DhText();
       DhText t2 = new DhText();
       t1.setColor( Color.RED );
       t1.setFont( "arial");
       t2.setColor( Color.RED );
       t2.setFont( "arial");
    

You could, of course, use derivation to save yourself time. For example, you might consider improving this with the following code.

       // Accomplish this task with derivation.
       public class MyText extends DhText
       {
          public MyText()
          {
              setColor( Color.RED );
              setFont( "arial" );
          }
    

This works fine until you decide you also want those settings for buttons, labels, tabs, documents, and so on. And you’ll find yourself with even more work when you apply these to another part of your program or to another program.

The answer to this problem is a Style object. While using this library, you can instantiate a Style object and set its properties at any point.

       // STEP 1: Create style objects.

       DhStyle myStyle = new DhStyle();

       // STEP 2: Set properties on style objects.

       myStyle.setColor( Color.RED );
       myStyle.setFont( "arial" );
    

At any other time in the code, you can then apply that style to any number of elements.

       DhText t1 = new DhText();
       DhText t2 = new DhText();

       // STEP 3: Apply styles using the setStyle method.

       t1.setStyle( myStyle );
       t2.setStyle( myStyle );
    

To make changes of this kind in the future, the following line sets all instances of all elements with myStyle set on them to change a color.

       myStyle.setColor( Color.BLUE );
   

This functionality is available during runtime. Every time you make a change to the Style object, the DHTML runtime dynamically retrieves and updates all elements that Style object is applied to.

For more information, see Understanding Style Inheritance.

© 1999 Microsoft Corporation. All rights reserved. Terms of use.