1
Developing a Simple JClass LiveTable Pro Program

Writing a Simple Java Stand-Alone Application · Setting the Table Size

Adding Column Labels and Formatting the Table · Adding a Title

The Finished Application · Loading Properties from a Web Page · Proceeding From Here


Writing a Simple Java Stand-Alone Application

This chapter allows you to immediately try out JClass LiveTable Pro by compiling and running an example program, introducing some fundamental JClass LiveTable Pro programming concepts. This application displays information about employees in a fictitious company. The finished program, simple.class, is one of the sample files provided with JClass LiveTable Pro.

The data to be displayed is as follows:
Name Address Phone Number Position Salary
James Q. Doohan 1701 Planetia Blvd. Anytown, U.S.A.   President $245,000
John F. Kricfalusi 983 Nickelodeon Street Anytown, U.S.A. (999) 555-9876 Animator $10,000
Marc Lenard 6 Gene Crescent Anytown, U.S.A. (999) 555-1212 Telemarketer $10 / hr.
Hikaru I. Takei 134 Adelaide Street E. Suite 204 Anytown, U.S.A. (999) 594-1026 OEM Sales 50%
Melissa A. Truman 475 Woodview Line Anytown, U.S.A. (999) 555-9030 Technical Writer $50,250
Stephanie L. Truman 388 Appleby Road Anytown, U.S.A. (999) 555-2642 System Analyst $85,750
Bill West 1001 Spumco Way Anytown, U.S.A. (999) 555-9966 Announcer $17,500

A Basic Table Applet

table1.java is a very basic Java program that implements some JClass LiveTable Pro elements. This minimal Java program simply creates a table containing some basic formatting elements and some sample data.
1	import jclass.table.*;
2	import java.awt.*;
3	import java.applet.Applet;
4	public class table1 extends Applet
5	{
6	String cells[][] = {
7		{"James Q. Doohan","1701 Planetia Blvd.\nAnytown, U.S.A.",
		 "","President","$245,000"},
8		{"John F. Kricfalusi","983 Nickelodeon Street\nAnytown,
		 U.S.A.",
		 "(999) 555-9876","Animator","$1,000"},
9		{"Marc Lenard","6 Gene Crescent\nAnytown, U.S.A.",
		 "(999) 555-1212","Telemarketer","$10 / hr."},
10		{"Hikaru I. Takei",
		 "134 Adelaide Street E.\nSuite 204\nAnytown, U.S.A.",
		 "(999) 594-1026","OEM Sales","50%"},
11		{"Melissa A. Truman","475 Woodview Line\nAnytown, U.S.A.",
		 "(999) 555-9030","Technical Writer","$5,250"},
12		{"Stephanie L. Truman","388 Appleby Road\nAnytown, U.S.A.",
		 "(999) 555-2642","System Analyst","$85,750"},
13		{"Bill West","1001 Spumco Way\nAnytown, U.S.A.",
		 "(999) 555-9966","Announcer","$17,500"}
14	};
15	public void init() {
16		setLayout(new GridLayout(1,1));
17		JCTable table;
18		table = new JCTable();
19		table.setNumRows(7);
20		table.setNumColumns(5);
21		table.setCells(cells);
22		table.setBackground(JCTblEnum.ALL, 
		JCTblEnum.ALL, Color.yellow);
23		table.setForeground(JCTblEnum.ALL, JCTblEnum.ALL, Color.blue);
24		table.setPixelWidth(JCTblEnum.ALL, JCTblEnum.VARIABLE);
25		table.setPixelHeight(JCTblEnum.ALL, JCTblEnum.VARIABLE);
26		add(table);
27	        }
28	}
table1.java listing

Most of the code should be familiar to Java, C or C++ programmers. Here is a line-by-line explanation of what is happening in table1.java:

Lines 2-3 import the classes that table1.java uses. The external classes that comprise jclass.table.*, java.awt.* and java.applet.Applet are imported for use in table1.java. While java.awt.* and java.applet.Applet are standard classes that come with the Java JDK, the statement import jclass.table.* enables table1.java to utilize a number of definitions for interfaces and packages contained within JClass LiveTable Pro.

Line 4 initiates the class declaration for table1.java. The syntax used here is different than that used in C++, which does not have explicit syntax for defining interfaces. By declaring the file as public, table1.java can be used by code outside of the class package. Only one public class is permitted per file, and the filename must be given the name of the class; in this case: table1.java. The statement extends Applet makes table1.java a superclass of Applet.

Line 6 sets the cell values as a matrix of Strings.

Lines 7-13 contain the data to be displayed. The data is divided into a number of comma-delimited string statements. Addresses that are separated on two lines of text are divided by "\n", which denotes a carriage return within the cell.

Line 15 initializes the code to display the table. Line 16 sets the layout manager for this container. Line 17 initiates a static JCTable method for table1.java. Line 18 creates a new instance of a table incorporating JClass LiveTable Pro elements.

Lines 19 and 20 sets the table size in terms of rows and columns. Line 21 sets the value for the cells. Lines 22-23 set the cell colors. Lines 24-25 set the sizes for the cells. JCTblEnum.ALL is a constant that JClass LiveTable Pro uses to indicate all rows or columns. In the setBackground() and setForeground() calls, the color is set for all rows and all columns, i.e. all cells. JCTblEnum.VARIABLE is another constant that JClass LiveTable Pro uses to indicate that a particular dimension can vary as needed.

As it stands, table1.java does not do much other than display a colorful table containing some data. In the following sections, further elements are added to this basic example of Java code.

Inserting an Applet into a Web Page

To run a compiled Java applet, it must be inserted into a Web page. The next code listing displays the sample HTML code necessary to insert table1.java into a Web page, called table1.html.
<HTML>
<HEAD>
<TITLE>Table 1</TITLE>
</HEAD>
<BODY>
<H1>Table 1</H1>
<APPLET CODE=table1.class HEIGHT=300 WIDTH=500>
</APPLET>
</BODY>
</HTML>
table1.html - HTML code necessary to display the table1 applet

After table1.html has been created, you can view the compiled table1.java applet by pointing your Java-capable Web browser to table1.html. The table1 applet will appear in the Web page, as seen in the next illustration.

If you do not have a Java-capable browser, you can use appletviewer to run Java applets. Go to the directory containing table1.html, then run appletviewer by typing the following at the prompt:

     appletviewer table1.html
When table1.java is compiled and run using appletviewer or through a Java-compatible Web browser, the window shown in the next image is displayed.

The applet displayed by table1.java

Basic Stand-Alone Table Application

table1m.java is a basic stand-alone Java application that implements some JClass LiveTable Pro elements. The code for table1m.java is listed in the following illustration. This minimal Java program creates a table containing some formatting elements containing sample data, and is functionally equivalent to table1.java.
1	import jclass.table.*;
2	import java.awt.*;
3	import java.applet.Applet;
4	public class table1m extends Applet {
5	String cells[][] = {
6		{"James Q. Doohan","1701 Planetia Blvd.\nAnytown, U.S.A.", 
		 "","President","$245,000"},
7		{"John F. Kricfalusi",
		"983 Nickelodeon Street\nAnytown, U.S.A.", 
		"(999) 555-9876","Animator","$1,000"},
8		{"Marc Lenard","6 Gene Crescent\nAnytown, U.S.A.", 
		"(999) 555-1212","Telemarketer","$10 / hr."},
9		{"Hikaru I. Takei",
		"134 Adelaide Street E.\nSuite 204\nAnytown, U.S.A.",
		"(999) 594-1026","OEM Sales","50%"},
10		{"Melissa A. Truman","475 Woodview Line\nAnytown, U.S.A.", "(999) 555-9030","Technical Writer","$5,250"},
11		{"Stephanie L. Truman","388 Appleby Road\nAnytown, U.S.A.",
		"(999) 555-2642","System Analyst","$85,750"},
12		{"Bill West","1001 Spumco Way\nAnytown, U.S.A.", 
		 "(999) 555-9966","Announcer","$17,500"}
13	};
14	public void init() {
15		setLayout(new GridLayout(1,1));
16		JCTable table = new JCTable();
17		table.setNumRows(7);
18		table.setNumColumns(5);
19		table.setCells(cells);
20		table.setBackground(JCTblEnum.ALL, JCTblEnum.ALL,
		Color.yellow);
21		table.setForeground(JCTblEnum.ALL, JCTblEnum.ALL, Color.blue);
22		table.setPixelWidth(JCTblEnum.ALL, JCTblEnum.VARIABLE);
23		table.setPixelHeight(JCTblEnum.ALL, JCTblEnum.VARIABLE);
24		add(table);
25	}
26	public static void main(String args[]) {
27		Frame f = new Frame();
28		table1m t = new table1m();
29		t.init();
30		f.setLayout(new GridLayout(1,1));
31		f.add(t);
32		f.pack();
33		f.show();
34      	}
35	}
Source code for the stand-alone Java application table1m.java

The source code for table1m.java contains many of the same elements as table1.java. The main differences begin at line 26, where the main argument is introduced. The Java code that follows creates the display environment for the code contained in the rest of the program.

Line 27 creates a frame to contain the program, while lines 28-29 create and initialize the previously derived applet code. Line 30 sets the layout manager for the frame, and line 31 adds the applet code to the frame of the stand-alone application. Lines 32-33 display the running applet within the frame.

When table1m.java is compiled and then run using java from your system's prompt, the window shown in the next illsutartion is displayed.

The stand-alone Java application table1m.java


Setting the Table Size

All properties for a table can be specified when you create the table, or they may be changed at any time as the program runs by using event listeners, which operate in a similar way to callbacks in C++. Each property has two accessor methods: set and get. Property values are set using set(PropertyName), and you can retrieve the current value of any property using the property's get method.

The number of rows and columns that appear in the Java application are set by table.setNumRows() and table.setNumColumns(). To create a 10 x 10 grid, you would change the values for setNumRows() and setNumColumns() to the following in table1.java:

	table.setNumRows(10);
	table.setNumColumns(10);


Adding Column Labels and Formatting the Table

The table displayed by the table1.java program is not very useful to an end-user. There are no column labels, and the table would look better with additional formatting elements.

Setting Labels

To add column labels to the table code for table1.java, add the following line immediately after line 26 in the initial code listing:
	table.setColumnLabels(labels);
To visually differentiate label cells from the other cells, different background and foreground colors need to be specified.

There are thirteen AWT color constants that can be used in Java. The color constant values are:

      Color.black                         Color.magenta
      Color.blue                          Color.orange
      Color.cyan                          Color.pink
      Color.darkGray                      Color.red
      Color.gray                          Color.white
      Color.green                         Color.yellow
      Color.lightGray
To set the background color of the labels to blue, and the foreground color (text) to white, insert the following lines:
     table.setBackground(JCTblEnum.LABEL, JCTblEnum.ALL, Color.blue);
     table.setForeground(JCTblEnum.LABEL, JCTblEnum.ALL, Color.white);

Changing Alignment

Another way to visually differentiate the text that appears within a table is to change its alignment within a cell relative to the default text alignment in other cells. By default, text (or anything else you insert into specific cells in a table) is horizontally centered and shifted to the left margin of the cell. If you want to set the labels in table1.java to appear horizontally centered and at the top of the cell, insert the following line:
     table.setAlignment(JCTblEnum.LABEL, JCTblEnum.ALL, JCTblEnum.TOPCENTER);

Changing the Fonts

It is also possible to change fonts and their appearance. This is another way to visually distinguish one part of a table from another, or to change the overall appearance of the table.

Java defines five different platform-independent font names that are found (or have close equivalents) on most computer platforms. Valid Java AWT font names are:

Note: Font names are case-sensitive.

There are also four standard font style constants that can be used. Valid Java AWT font style constants are:

To change the text column labels in table1.java from their default to display in italic Times Roman, insert the following line:
     table.setFont(JCTblEnum.LABEL, JCTblEnum.ALL,
     new Font("TimesRoman", Font.ITALIC, 20));
Note: The type of font displayed on a user's system depends entirely on the fonts that are local to that user's computer. If a font name specified in a Java program is not found on a user's system, the closest possible match is used (as determined by the Java AWT).


Formatting Parts of the Table

It is easy for a user to pick out specific information in this new table by using the column labels. In some cases, you will want the information in a certain cell to stand out from the rest. The following code, when added to the improved table1.java, highlights the President's salary by setting it to a different foreground color:
 
     table.setBackground(0, 3, Color.red);
     table.setForeground(0, 3, Color.yellow);
The improved table1.java, including the code listed above, appears in code example table3.java that comes with this product. It can be run from a Java-capable Web browser or through appletviewer by pointing the application to table3.html.


Adding a Title

Most tables are more useful and easier to spot amongst other tables if they have a title. To add a title to a table, add the JCTitleLayout component to the top of the table component code above the lines specifying column labels, as depicted in the following code listing:
1	public void init() {
2		JCTitleLayout jct = new JCTitleLayout();
3		setLayout(jct);
4		label.setBackground(Color.cyan);
5		label.setForeground(Color.black);
6		label.setFont(new Font("TimesRoman", Font.BOLD, 20));
7		add("Top", label);
8		Insets topoff = new Insets(5, JCTitleLayout.NOVALUE, 
9					 	10, JCTitleLayout.NOVALUE);
10		jct.setOffsets("Top", topoff);
Adding a title to table3.java

Line 1 is from the already existing code for table3.java.

Lines 2-3 create a title layout as the default layout. Lines 4-5 set the background of the title to cyan, and the foreground (text) of the title to black. Line 6 sets the font and font characteristics of the title text. Line 7 adds the title to the top of the label structure.

Lines 8-10 inserts the item in the top area and offsets it 5 pixels from the center item and 10 pixels from the top. It is also horizontally centered.

Code example table4.java contains the Java code for table3.java plus the title code listed in the previous code listing. It can be run from a Java-capable Web browser or through appletviewer by pointing the application to table4.html.

Overall Table Formatting

When compiled and viewed, the improved table1.java looks like the display in the next illustration:

table1.java with data and basic improvements (table4.java)

These improvements have been added to the available source code example table4.java.


The Finished Application

All of the improvements to table1.java made in this chapter are combined in simple.java. This and the others described in the section appear in the directory /jclass/table/examples/. When simple.java is compiled and executed, the window shown in the next figure is displayed.

The simple.java window


Loading Properties from a Web Page

A common task in any JClass LiveTable Pro program is to load the text data (the cell values) in a format that JClass LiveTable Pro understands. In most cases, the cell values and other properties are contained within the body of the source code.

There are some cases when it is more desirable to have the properties located outside of the source code of the Java program. The chief advantage of this method is that it allows the properties to be changed using a simple editing program. This saves having to recompile the Java program whenever properties need to be altered.

To specify that the table should load properties from its HTML page, create the table with the applet in its constructor.

The source code for simple_html.java, illustrated in the following code listing, shows the Java code necessary to load the properties from a separate HTML file. The resulting applet functions and displays in exactly the same way as table4.java

import jclass.table.*;
public class simple_html extends java.applet.Applet
{
public void init() {
	add(new JCTable(this, null));
}
}
simple_html.java creates the table and loads the properties from simple.html, illustrated in the following code listing:
<HTML>
<HEAD>
<TITLE>Simple</TITLE>
<!-- @(#)simple.html	1.1	96/05/27	KL Group Inc. -->
</HEAD>
<BODY>
<H1>Simple</H1>
<APPLET CODE=simple_html.class HEIGHT=300 WIDTH=650>
<PARAM NAME=columnLabels VALUE="Name|Address|Phone Number|Position|
Salary">
<PARAM NAME=cells VALUE="
	(James Q. Doohan|1701 Planetia Blvd.\nAnytown, U.S.A.||
President|$245,000)
	(John F. Kricfalusi|983 Nickelodeon Street\nAnytown, U.S.A.|
\(999\) 555-9876|
Animator|$1,000)
	(Marc Lenard|6 Gene Crescent\nAnytown, U.S.A.|
\(999\) 555-1212|Telemarketer|$10 / hr.)
	(Hikaru I. Takei|
134 Adelaide Street E.\nSuite 204\nAnytown, U.S.A.|\(999\) 594-1026|
OEM Sales|50%)
	(Melissa A. Truman|475 Woodview Line\nAnytown, U.S.A.|
\(999\) 555-9030|Technical Writer|$5,250)
	(Stephanie L. Truman|388 Appleby Road\nAnytown, U.S.A.|
\(999\) 555-2642|System Analyst|$85,750)
	(Bill West|1001 Spumco Way\nAnytown, U.S.A.|\(999\) 555-9966|
Announcer|$17,500)">
<PARAM NAME=Foreground VALUE="(ALL ALL blue)(label all white)
(0 4 red)">
<PARAM NAME=Background VALUE="(ALL ALL yellow) (LABEL ALL blue)">
<PARAM NAME=PixelHeight VALUE="(ALL variable)">
<PARAM NAME=PixelWidth VALUE="(ALL variable)">
<PARAM NAME=Font VALUE="(label all timesroman-italic-20)">
<PARAM NAME=Alignment VALUE="(label ALL topcenter)">
</APPLET>
</HTML>
simple.html, containing the properties retrieved by simple_html.java

simple.html is an HTML file containing all of the properties displayed by the compiled simple_html.java applet. The Java applet is inserted within simple.html using the <APPLET> pair of tags, similar to the HTML code for table1.html seen early in this chapter. All of the properties are loaded using <PARAM NAME= ... VALUE= ...> pairs, its data delimited by the pipe character ("|").1 For more information on the valid parameters for the <APPLET> set of tags, consult an HTML guide.

While this method of inserting data into a Java applet provides greater flexibility in terms of editing its properties, it also has the disadvantage of running more slowly than the equivalent applet that contains the properties within the body of the program. Use the method that is appropriate to your needs and situation.

Any values set programmatically override HTML values. properties that are essential to the operation of your program should be set within the body of the program. You can then allow your users to customize other properties, such as colors.


End-User Interaction

More than simply a display tool, JClass LiveTable Pro is an interactive component. By default, end-users can interact with the component in the following ways:


Proceeding From Here

The following suggestions should help you be productive with JClass LiveTable Pro as quickly as possible: Important: If you are using the Lite version of JClass LiveTable Pro, substitute all occurrences of "JCTable" with "JCTableLite" within the code examples displayed in this manual, and in any Java code you write that uses JClass LiveTable Lite components.


1 Any angle bracket characters ("<" and ">") that are to be displayed within a cell need to be escaped by placing a backslash ("\") character immediately before the angle bracket character. This is because angle brackets are normally reserved HTML characters, denoting the beginning or end of an HTML tag.