![]() ![]() ![]() |
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:
- Returns a trimmed String, with trailing nulls removed.
- Converts a String to an integer.
- Converts a String to a double.
- Converts a String to a Boolean.
- Converts a String to an array of
Strings.
- Converts a String to an array of integers or
Integer
objects.- Converts a String to an array of
Double
objects.- Converts all occurrences of
"\n"
to newlines.- Converts all occurrences of
char '\n'
toString
"\n".
- Converts a delimited list of
String
tokens to aVector.
- Converts a String to an enum, or a list of enums.
- Converts an enum to a String.
- Converts an object to a String.
- Converts a
String
to aDate.
- Removes "escape" characters (backslashes) from the String.
- Allows parsing errors to be printed or shown in a dialog.
27.2 Features of JCSwingTypeConverter
JCSwingTypeConverter
can perform these functions:
- Converts a String to a
Color
, or an array ofColors.
- Converts color to one of the
Color
enums, or RGB format.- Converts list to a comma-separated list of tokens.
- Converts a font name to a font instance, or a
Font
to a name-style-sizeString
, or aString
like Helvetica-plain-10 to aFont.
- Converts a String to an
Insets
instance, or creates aString
from an AWTInsets
value.- Converts a String to a
Dimension
instance.- Converts a String to a
Point
instance.
27.3 Classes
The two type converter classes are
com.klg.jclass.util.JCTypeConverter
andcom.klg.jclass.util.swing.JCSwingTypeConverter
. Both contain static methods for converting from one standard type to another.JCTypeConverter
is for converting Java types, andJCSwingTypeConverter
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:JCSwingTypeConverter
The methods in
JCSwingTypeConverter
are:
27.5 Examples
The following example gives you an indication of how the static methods in
import java.awt.Dimension;JCSwingTypeConverter
can be used.
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
import java.util.Date;JCTypeConverter
are called in a similar fashion, as illustrated next.
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());
}
}
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
![]() ![]() ![]() |