JClass Elements

PreviousNextIndex

27

Type Converters

Features of JCTypeConverter  Features of JCSwingTypeConverter

Classes  Methods   Examples 


27.1 Features of JCTypeConverter

There is frequently a need to convert objects to Strings and Strings to objects when you are coding a user interface. For example, a user types a String as input that you would like to convert to an object. The input String might consist of a sequence of integers, delimited by commas, that you would like to convert to an array. There are also times when you need to convert an object to a String so that you can place the text on a label or a button. The JClass type converters are a collection of the most useful conversions from String to object, and from object to String. The static methods of JCTypeConverter let you retrieve parameters from an application or applet and convert these parameters to particular data types.

JCTypeConverter performs these functions:


27.2 Features of JCSwingTypeConverter

JCSwingTypeConverter can perform these functions:


27.3 Classes

The two type converter classes are com.klg.jclass.util.JCTypeConverter and com.klg.jclass.util.swing.JCSwingTypeConverter. Both contain static methods for converting from one standard type to another. JCTypeConverter is for converting Java types, and JCSwingTypeConverter is for Swing types.


27.4 Methods

JCTypeConverter

JCTypeConverter contains static methods for retrieving parameters from a source file or applet, and for converting parameters to particular data types.

The methods in JCTypeConverter are:

checkEnum()

Checks the validity of an enum.

error()

Writes a parse error message to the standard output device.

fromEnum()

Converts an enum to a String.

fromNewLine()

Converts all occurrences of char '\n' to String "\n"

removeEscape()

Removes escape characters (backslashes) from the String.

toBoolean()

Converts a String to a Boolean. The method takes two parameters: the String representation of the Boolean, and a boolean default value to use if a parse error occurs.

toDate()

Converts a String to a Date.

toDouble()

Converts a String to a double. The method takes two parameters: the String representation of the number, and a Double default value to use if a parse error occurs.

toDoubleList()

Converts a String to an array of Double objects based on the provided delimiter. An optional third parameter is the default value, returned if a parse error occurs.

toEnum()

Converts a String to an enum. If the String cannot be converted, an error message is written to the console. The first three of its six parameters are the String to be converted, the enum type specified as a String, and a PARAM name for the enum (used in an error message). The next two are two-dimensional arrays that link enum types and their corresponding values. The last parameter is the value that should be returned it the Strings cannot be converted.

The method has other signatures as well. See the API for details.

toEnumList()

Converts a String to a list of enums. If the String cannot be converted, an error message is written to the console.

toInt()

Converts a String to an integer. The method takes two parameters: the String representation of the integer, and an integer default value to use if a parse error occurs.

toIntegerList()

Converts a String to an array of Integers based on the provided delimiter. An optional third parameter is the default value, returned if a parse error occurs.

toIntList()

Converts a String to an array of Integer objects based on the provided delimiter. An optional third parameter is the default value, returned if a parse error occurs.

toNewLine()

Converts all occurrences of "\n" to newlines.

toString()

Converts an object to a String. If a String, newlines are replaced by "\n". If a Vector, it is converted to a comma-separated list.

toStringList()

Converts a String to an array of Strings. There are three signatures: (a) a comma-separated String, (b) a String and a delimiter, and (c) the String, delimiter, and a Boolean indicating whether the String should be trimmed. Strings are trimmed by default.

toVector()

Converts a delimited list of tokens to a Vector. The second parameter is the delimiter that separates the tokens in the String. An optional third parameter is the default value, returned if a parse error occurs.

trim()

Returns a trimmed String, with trailing nulls removed.

JCSwingTypeConverter

The methods in JCSwingTypeConverter are:

toInsets()

Converts a String to an Insets instance.

fromInsets()

Creates a String from an AWT Insets value.

toDimension()

Converts a String in the form "40x30" to a Dimension instance.

toPoint()

Converts a String to a Point instance.

toColorList()

Converts a String to an array of Colors. An optional second parameter allows you to specify a default Color list if an error occurs while parsing the String.

toColor()

Converts a String to a Color. An optional second parameter allows you to specify a default Color if an error occurs while parsing the String.

fromColorList()

Converts list to a comma-separated list of tokens, to one of the Color enums, or to RGB format.

toFont()

Converts a font name to a font instance, or a font name in format "name-style-size", for instance, "Helvetica-plain-10."

fromFont()

Returns font in format "name-style-size."


27.5 Examples

The following example gives you an indication of how the static methods in JCSwingTypeConverter can be used.

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Color;
import com.klg.jclass.util.swing.JCSwingTypeConverter;

class SwingTypeConverterExamples {

public static void main(String[] args){
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++");
String s;
Font f = new Font("System", 10, 10);
s = JCSwingTypeConverter.fromFont(f);
System.out.println("The name of the font is " + s);

System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++");

String colors = "red, blue, green";
Color[] colorarray = JCSwingTypeConverter.toColorList(colors, null);
for (int i=0; i<colorarray.length; i++)
System.out.println("The array of colors is: " + colorarray[i].toString(
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++");

Color[] mycolors = JCSwingTypeConverter.toColorList(new String ("black, blue, cyan"));
for (int i=0; i<mycolors.length; i++)
System.out.println("The Color array is: " + mycolors[i].toString());

System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++");
Color yourcolor = JCSwingTypeConverter.toColor("darkGray", Color.gray);
System.out.println("The color is: " + yourcolor.toString());

System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++");
Dimension dim = JCSwingTypeConverter.toDimension("40x30", null);
System.out.println("The dimension is: " + dim.toString());

}
}

The output of this program is:

+++++++++++++++++++++++++++++++++++++++++++++++++++
The name of the font is System-PLAIN-10
+++++++++++++++++++++++++++++++++++++++++++++++++++
The array of colors is: java.awt.Color[r=255,g=0,b=0]
The array of colors is: java.awt.Color[r=0,g=0,b=255]
The array of colors is: java.awt.Color[r=0,g=255,b=0]
+++++++++++++++++++++++++++++++++++++++++++++++++++
The Color array is: java.awt.Color[r=0,g=0,b=0]
The Color array is: java.awt.Color[r=0,g=0,b=255]
The Color array is: java.awt.Color[r=0,g=255,b=255]
+++++++++++++++++++++++++++++++++++++++++++++++++++
The color is: java.awt.Color[r=64,g=64,b=64]
+++++++++++++++++++++++++++++++++++++++++++++++++++
The dimension is: java.awt.Dimension[width=40,height=30]

The static methods of JCTypeConverter are called in a similar fashion, as illustrated next.

import java.util.Date;
import java.text.DateFormat;
import com.klg.jclass.util.JCTypeConverter;

class TypeConverterExamples {

public static void main(String[] args){
System.out.println("+++++++++++++++++++++++++++++++++++++++++");
String s = "10.777";
double dd = 10;
double d = JCTypeConverter.toDouble(s, dd);
System.out.println("The value of the double is: " + d);

System.out.println("+++++++++++++++++++++++++++++++++++++++++");

s = "Abel, Ben, Curry, Dave";
String[] sa = JCTypeConverter.toStringList(s, ',', true);
for (int i=0; i<sa.length; i++)
System.out.println("The array element is: " + sa[i]);
System.out.println("+++++++++++++++++++++++++++++++++++++++++");

s = "1, 1, 2, 3, 5, 8, 13";
int [] da = {1,1,1,1,1,1,1};
int[] ii = JCTypeConverter.toIntList(s, ',', da);
for (int i=0; i<ii.length; i++)
System.out.println("The Integer array element is: " + ii[i]);

System.out.println("+++++++++++++++++++++++++++++++++++++++");
s = "Feb 30, 2000";
Date today = new Date("June 12, 1999");
Date myDate = JCTypeConverter.toDate(s, today);
System.out.println("The date is: " + myDate.toString());

}
}

Here is the output:

DOS: %JAVA_HOME%\bin\java TypeConverterExamples
+++++++++++++++++++++++++++++++++++++++++
The value of the double is: 10.777
+++++++++++++++++++++++++++++++++++++++++
The array element is: Abel
The array element is: Ben
The array element is: Curry
The array element is: Dave
+++++++++++++++++++++++++++++++++++++++++
The Integer array element is: 1
The Integer array element is: 1
The Integer array element is: 2
The Integer array element is: 3
The Integer array element is: 5
The Integer array element is: 8
The Integer array element is: 13
+++++++++++++++++++++++++++++++++++++++++
The date is: Sat Jun 12 00:00:00 EDT 1999


PreviousNextIndex