replace Swing AttributeSet - Java javax.swing.text

Java examples for javax.swing.text:AttributeSet

Description

replace Swing AttributeSet

Demo Code


//package com.java2s;
import java.util.Enumeration;
import javax.swing.text.AttributeSet;
import javax.swing.text.MutableAttributeSet;

public class Main {
    public static void replace(MutableAttributeSet a, String attributeName,
            String attributeValue) {
        Enumeration en = a.getAttributeNames();
        do {//from w ww. ja  v  a 2s . c o m
            if (!en.hasMoreElements()) {
                break;
            }
            Object name = en.nextElement();
            if (name.toString().equalsIgnoreCase(attributeName)) {
                a.removeAttribute(name);
                a.addAttribute(name, attributeValue);
            }
        } while (true);
    }

    public static String toString(AttributeSet a) {
        StringBuilder builder = new StringBuilder("[");
        Enumeration en = a.getAttributeNames();
        do {
            if (!en.hasMoreElements()) {
                break;
            }
            Object name = en.nextElement();
            builder.append(name).append("=").append(a.getAttribute(name));
            if (en.hasMoreElements()) {
                builder.append(", ");
            }
        } while (true);
        builder.append("]");
        return builder.toString();
    }
}

Related Tutorials