Java Utililty Methods Swing Text Style

List of utility methods to do Swing Text Style

Description

The list of methods to do Swing Text Style are organized into topic(s).

Method

StylecreateStyle(String logicalName)
create Style
Style result = styleContext.addStyle(logicalName, null);
return result;
BooleangetBooleanValue(Element element, Object constant)
Checks whether an Element has a boolean property
AttributeSet attributes = element.getAttributes();
return (attributes.getAttribute(constant) != null
        && ((Boolean) attributes.getAttribute(constant)).booleanValue());
IntegergetIntegerValue(Element element, Object constant)
Gets a integer value of an attribute (e.g.
AttributeSet attributes = element.getAttributes();
Object obj = attributes.getAttribute(constant);
if (obj == null)
    return null;
return (Integer) obj;
intgetNextVisualPosition(final View v, final int pos, final Position.Bias b0, final int direction, final Position.Bias[] biasRet)
get Next Visual Position
boolean toWest = (direction == SwingConstants.WEST);
Document document = v.getDocument();
int length = document.getLength();
if (!isBidirectional(document)) {
    return getTrivialVisualPosition(toWest, pos, b0, length, biasRet, true);
Element bidiRoot = ((AbstractDocument) document).getBidiRootElement();
Element elem = getElementByPosition(bidiRoot, pos);
...
booleanhasDifferentCharacterAttributes(AttributeSet style, AttributeSet base)
has Different Character Attributes
return (StyleConstants.isBold(style) != StyleConstants.isBold(base))
        || (StyleConstants.isItalic(style) != StyleConstants.isItalic(base))
        || (StyleConstants.isUnderline(style) != StyleConstants.isUnderline(base))
        || (StyleConstants.getFontSize(style) != StyleConstants.getFontSize(base))
        || (!StyleConstants.getForeground(style).equals(StyleConstants.getForeground(base)));
booleanisCentered(AttributeSet attributes)
is Centered
if (attributes != null) {
    if (attributes.getAttribute(StyleConstants.Alignment) != null) {
        Integer value = (Integer) attributes.getAttribute(StyleConstants.Alignment);
        if (value == StyleConstants.ALIGN_CENTER) {
            return true;
return false;
booleanisLTR(final int level)
is LTR
return (level & 1) == 0;
voidoverrideStyle(Style originalStyle, Style newStyle)
override Style
overrideStyleElement(originalStyle, newStyle, StyleConstants.FontFamily);
overrideStyleElement(originalStyle, newStyle, StyleConstants.Family);
overrideStyleElement(originalStyle, newStyle, StyleConstants.FontSize);
overrideStyleElement(originalStyle, newStyle, StyleConstants.Size);
overrideStyleElement(originalStyle, newStyle, StyleConstants.Foreground);
overrideStyleElement(originalStyle, newStyle, StyleConstants.Background);
overrideStyleElement(originalStyle, newStyle, StyleConstants.Bold);
overrideStyleElement(originalStyle, newStyle, StyleConstants.Italic);
...
AttributeSetresolveAttributes(AttributeSet style)
resolve Attributes
SimpleAttributeSet set = new SimpleAttributeSet();
if (style != null) {
    Enumeration<?> names = style.getAttributeNames();
    Object value;
    Object key;
    while (names.hasMoreElements()) {
        key = names.nextElement();
        value = style.getAttribute(key);
...
StringunRTF(String s)
Extract text from RTF
if (s == null)
    return null;
DefaultStyledDocument doc = new DefaultStyledDocument();
RTFEditorKit kit = new RTFEditorKit();
try {
    kit.read(new StringReader(s), doc, 0);
} catch (Exception ex) {
    throw new RuntimeException(ex);
...