#### Example 1: Accessing a record for a UDO and registering the UDO with the report system

This example describes how to access a record for your UDO and how to register your UDO with the report system.

1. A UDO needs to implement at least four classes from JReport. The four classes are for template, result, creator and render files respectively.
   - MyDbFld.java

         //Template
         package myudo;

         import jet.report.*;
         import jet.controls.*;

         /**
          * This class extends form JRObjectTemplate, add two properties "ColumnName" and
          * "TextColor".
          */
         public class MyDbFld extends JRObjectTemplate {
                 /**
                  * This property is for the column name of MyDbFld.
                  */
                 public JetColumnName columnName = new JetColumnName(this, "ColumnName");

                 /**
                  * This property is for the background color of MyDbFld..
                  */
                 public JetColor backColor = new JetColor(this, "TextColor", null, true);

                 public MyDbFld() {
                         super();
                         columnName.setEditable(true);
                         // set the default size.
                         set("Width", 40); // before build575 should be width.set(40);
                         set("Height", 20); // before build575 should be height.set(20);
                         // add the property to group then they can displayed in ReportInspector.
                         addPropertyToGroup("ColumnName", "Others");
                         addPropertyToGroup("TextColor", "Color");
                 }

                 /**
                  * Return the instance name prefix. >
                  */
                 public String getPrefix() {
                         return "MyDbField";
                 }
         }

   - MyDbFldRst.java

         //Result
         package myudo;

         import jet.connect.Record;
         import jet.connect.DbValue;
         import jet.util.*;
         import jet.datastream.*;

         public class MyDbFldRst extends JRVisiableResult {
                 public MyDbFldRst() {
                 }

                 // retrieve value from column.
                 DbValue getValue() {
                         // get the column name.
                         String colName = (String) getPropertyByName("ColumnName").getObject();
                         // get the report record.
                         Record record = getRecord();
                         // return the column value.
                         return record.getCell(colName);
                 }

                 // make the text will be displayed.
                 String getText() {
                         String sRet;
                         DbValue value = getValue();
                         if (value != null && !value.isNull()) {
                                 sRet = value.toString();
                         } else {
                                 sRet = "NULL";
                         }
                         return sRet;
                 }
         }

   - MyDbFldCreator.java

         //Creator
         package myudo;

         import java.awt.*;
         import java.util.*;
         import jet.report.*;
         import jet.controls.*;
         import jet.connect.*;
         import jet.udo.*;
         import jet.datastream.*;
         import guitools.Painter;
         import jet.util.*;

         /**
          * Implements JRObjectResultCreator for creating JRObjectResult in JReport
          * Engine Bean.
          */
         public class MyDbFldCreator implements JRObjectResultCreator {
                 public MyDbFldCreator() {
                 }

                 public JRObjectResult createJRObjectResult(JRObjectTemplate rptobj,
                                 Record record) {
                         MyDbFld rpt = (MyDbFld) rptobj;
                         MyDbFldRst dsField = new MyDbFldRst();
                         dsField.setTemplate(rpt);
                         return dsField;
                 }
         }

   - MyDbFldRender.java

         //Render
         package myudo;

         import java.awt.*;
         import java.awt.event.*;
         import java.util.*;
         import jet.datastream.*;
         import jet.connect.DbValue;
         import jet.connect.Record;
         import jet.udo.*;

         public class MyDbFldRender extends Component implements JRObjectRender {
                 String text = null;

                 Color color;

                 Color background;

                 /**
                  * The default constructor.
                  */
                 public MyDbFldRender() {
                 }

                 // retrieve property and data from JRObjectResult.
                 public void setProperty(jet.util.PropertySetable dsPropSet) {
                         text = ((MyDbFldRst) dsPropSet).getText();
                         color = (Color) dsPropSet.getPropertyByName("TextColor").getObject();
                         background = (Color) dsPropSet.getPropertyByName("Background")
                                         .getObject();
                         setBounds(((MyDbFldRst) dsPropSet).getBounds());
                 }

                 /**
                  * Paint the text.
                  */
                 public void paint(Graphics g) {
                         Dimension dim = getSize();
                         if (background != null) {
                                 g.setColor(background);
                                 g.fillRect(0, 0, dim.width, dim.height);
                         }
                         if (text != null) {
                                 if (color == null)
                                         color = Color.black;
                                 g.setColor(color);
                                 g.drawString(text, 10, 10);
                         }
                 }
         }

     Note: Since the compiling process of JReport Designer differs with that of JReport Enterprise Server, when you run UDOs in these two applications, you should pay attention to some minor differences. For example, to get width and height, in JReport Designer, you should use:
     `w = guitools.toolkit.Unit.convertUnitToPixel(((Integer)propertySetable.getPropertyByName ("Width").getObject()).intValue());
     h = guitools.toolkit.Unit.convertUnitToPixel(((Integer)propertySetable.getPropertyByName ("Height").getObject()).intValue());`

     While in JReport Enterprise Server, you should use the following instead:
     `JRObjectResult obj = (JRObjectResult)propertySetable;
     w = guitools.toolkit.Unit.convertUnitToPixel(obj.getTemplate().getWidth(obj));
     h = guitools.toolkit.Unit.convertUnitToPixel(obj.getTemplate().getHeight(obj));`

2. Compile the Java files.

   To compile these four Java files, you should add report.jar and JREngine.jar with their path into the class path. For example, use the following command:

   `Javac -classpath "C:\JReport\Designer\lib\JRengine.jar;C:\JReport\Designer\lib\report.jar;C:\test "MyDbFld.java`

   Here it is assumed that JReport Designer is installed to `C:\JReport\Designer`. The Java files for the example are in `C:\test\myudo`.

3. Modify the udo.ini file in the `<install_root>\lib` directory by appending the four classes as follows:

       Begin
       name=MyDbField
       template=myudo.MyDbFld
       resultobject=myudo.MyDbFldRst
       resultcreator=myudo.MyDbFldCreator
       resultviewer=myudo.MyDbFldRender
       End

4. Edit JReport.bat in `<install_root>\bin`, append the four classes to the class path. Assume that the four classes are located in `d:\test\myudo`.

   Change: `... -classpath "c:\jreport\designer\lib\report.jar; ...c:\temp"`...
   To : `... -classpath "c:\jreport\designer\lib\report.jar;d:\test; ... c:\temp"` ...

5. Start JReport Designer with the modified batch file.
6. Click Insert > UDO. You will now find a new UDO object named MyDbFld in the drop-down list of the Insert UDO dialog.