![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e308. Adding an Attribute to a StringSome applications need to mark a range of characters in a string with an attribute, such as a color. TheAttributedString class is a
wrapper for a string that provides support for marking ranges of
characters with an attribute. An attribute consists of a name, a
value, and a contiguous range of characters on which the attribute
applies.
This example marks a word in a string with the attribute called
// Declare an attribute name. // An attribute name is an object that extends AttributedCharacterIterator.Attribute. // Author's note: A more appropriate name would be AttributedCharacterIterator.AttributeName static final AttributedCharacterIterator.Attribute COLOR = new AttributedCharacterIterator.Attribute("color") { }; // Create the attributed string AttributedString astr = new AttributedString("the hot pot"); // Add the COLOR attribute on the word `hot' astr.addAttribute(COLOR, "Red", 4, 7);
e306. Comparing Strings in a Locale-Independent Way e307. Iterating the Characters of a String e309. Incrementing a Double by the Smallest Possible Amount e310. Localizing Messages
© 2002 Addison-Wesley. |