StyleContext.NamedStyle (Java SE 26 & JDK 26)
Description: declaration: module: java.desktop, package: javax.swing.text, class: StyleContext, class: NamedStyle
Keywords: javax.swing.text.StyleContext.NamedStyle class

# Class StyleContext.NamedStyle

java.lang.Object
javax.swing.text.StyleContext.NamedStyle

All Implemented Interfaces:
  `Serializable, AttributeSet, MutableAttributeSet, Style`

Enclosing class:
  `StyleContext`

public class StyleContext.NamedStyle extends Object implements Style, Serializable
A collection of attributes, typically used to represent character and paragraph styles. This is an implementation of MutableAttributeSet that can be observed if desired. These styles will take advantage of immutability while the sets are small enough, and may be substantially more efficient than something like SimpleAttributeSet.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the `java.beans` package. Please see `XMLEncoder`.

- ## Nested Class Summary

  ### Nested classes/interfaces declared in interface AttributeSet

  `AttributeSet.CharacterAttribute, AttributeSet.ColorAttribute, AttributeSet.FontAttribute, AttributeSet.ParagraphAttribute`
  Modifier and Type
  Interface
  Description
  `static interface`
  `AttributeSet.CharacterAttribute`
  This interface is the type signature that is expected to be present on any attribute key that contributes to character level presentation.
  `static interface`
  `AttributeSet.ColorAttribute`
  This interface is the type signature that is expected to be present on any attribute key that contributes to presentation of color.
  `static interface`
  `AttributeSet.FontAttribute`
  This interface is the type signature that is expected to be present on any attribute key that contributes to the determination of what font to use to render some text.
  `static interface`
  `AttributeSet.ParagraphAttribute`
  This interface is the type signature that is expected to be present on any attribute key that contributes to the paragraph level presentation.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected ChangeEvent`
  `changeEvent`
  Only one ChangeEvent is needed per model instance since the event's only (read-only) state is the source property.
  `protected EventListenerList`
  `listenerList`
  The change listeners for the model.

  ### Fields declared in interface AttributeSet

  `NameAttribute, ResolveAttribute`
  Modifier and Type
  Field
  Description
  `static final Object`
  `NameAttribute`
  Attribute name used to name the collection of attributes.
  `static final Object`
  `ResolveAttribute`
  Attribute name used to identify the resolving parent set of attributes, if one is defined.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `NamedStyle()`
  Creates a new named style, with a null name and parent.
  `NamedStyle(String name, Style parent)`
  Creates a new named style.
  `NamedStyle(Style parent)`
  Creates a new named style.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addAttribute(Object name, Object value)`
  Adds an attribute.
  `void`
  `addAttributes(AttributeSet attr)`
  Adds a set of attributes to the element.
  `void`
  `addChangeListener(ChangeListener l)`
  Adds a change listener.
  `boolean`
  `containsAttribute(Object name, Object value)`
  Checks whether a given attribute name/value is defined.
  `boolean`
  `containsAttributes(AttributeSet attrs)`
  Checks whether the element contains all the attributes.
  `AttributeSet`
  `copyAttributes()`
  Copies a set of attributes.
  `protected void`
  `fireStateChanged()`
  Notifies all listeners that have registered interest for notification on this event type.
  `Object`
  `getAttribute(Object attrName)`
  Gets the value of an attribute.
  `int`
  `getAttributeCount()`
  Gets the number of attributes that are defined.
  `Enumeration<?>`
  `getAttributeNames()`
  Gets the names of all attributes.
  `ChangeListener[]`
  `getChangeListeners()`
  Returns an array of all the `ChangeListener`s added to this NamedStyle with addChangeListener().
  `<T extends EventListener>
  T[]`
  `getListeners(Class<T> listenerType)`
  Return an array of all the listeners of the given type that were added to this model.
  `String`
  `getName()`
  Fetches the name of the style.
  `AttributeSet`
  `getResolveParent()`
  Gets attributes from the parent.
  `boolean`
  `isDefined(Object attrName)`
  Checks whether a given attribute is defined.
  `boolean`
  `isEqual(AttributeSet attr)`
  Checks whether two attribute sets are equal.
  `void`
  `removeAttribute(Object name)`
  Removes an attribute from the set.
  `void`
  `removeAttributes(Enumeration<?> names)`
  Removes a set of attributes for the element.
  `void`
  `removeAttributes(AttributeSet attrs)`
  Removes a set of attributes for the element.
  `void`
  `removeChangeListener(ChangeListener l)`
  Removes a change listener.
  `void`
  `setName(String name)`
  Changes the name of the style.
  `void`
  `setResolveParent(AttributeSet parent)`
  Sets the resolving parent.
  `String`
  `toString()`
  Converts the style to a string.

  ### Methods declared in class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  Creates and returns a copy of this object.
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `protected void`
  `finalize()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Finalization is deprecated and subject to removal in a future release.
  `final Class<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `int`
  `hashCode()`
  Returns a hash code value for this object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `final void`
  `wait()`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
  `final void`
  `wait(long timeoutMillis)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  `final void`
  `wait(long timeoutMillis, int nanos)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

- ## Field Details

  - ### listenerList

    protected EventListenerList listenerList
    The change listeners for the model.

  - ### changeEvent

    protected transient ChangeEvent changeEvent
    Only one ChangeEvent is needed per model instance since the event's only (read-only) state is the source property. The source of events generated here is always "this".

- ## Constructor Details

  - ### NamedStyle

    public NamedStyle(String name, Style parent)
    Creates a new named style.
    Parameters:
      `name` - the style name, null for unnamed
      `parent` - the parent style, null if none
    Since:
      1.4

  - ### NamedStyle

    public NamedStyle(Style parent)
    Creates a new named style.
    Parameters:
      `parent` - the parent style, null if none
    Since:
      1.4

  - ### NamedStyle

    public NamedStyle()
    Creates a new named style, with a null name and parent.

- ## Method Details

  - ### toString

    public String toString()
    Converts the style to a string.
    Overrides:
      `toString` in class `Object`
    Returns:
      the string

  - ### getName

    public String getName()
    Fetches the name of the style. A style is not required to be named, so null is returned if there is no name associated with the style.
    Specified by:
      `getName` in interface `Style`
    Returns:
      the name

  - ### setName

    public void setName(String name)
    Changes the name of the style. Does nothing with a null name.
    Parameters:
      `name` - the new name

  - ### addChangeListener

    public void addChangeListener(ChangeListener l)
    Adds a change listener.
    Specified by:
      `addChangeListener` in interface `Style`
    Parameters:
      `l` - the change listener

  - ### removeChangeListener

    public void removeChangeListener(ChangeListener l)
    Removes a change listener.
    Specified by:
      `removeChangeListener` in interface `Style`
    Parameters:
      `l` - the change listener

  - ### getChangeListeners

    public ChangeListener[] getChangeListeners()
    Returns an array of all the `ChangeListener`s added to this NamedStyle with addChangeListener().
    Returns:
      all of the `ChangeListener`s added or an empty array if no listeners have been added
    Since:
      1.4

  - ### fireStateChanged

    protected void fireStateChanged()
    Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.
    See Also:
      - `EventListenerList`

  - ### getListeners

    public <T extends EventListener> T[] getListeners(Class<T> listenerType)
    Return an array of all the listeners of the given type that were added to this model.
    Type Parameters:
      `T` - the listener type
    Parameters:
      `listenerType` - the type of listeners requested
    Returns:
      all of the objects receiving listenerType notifications from this model
    Since:
      1.3

  - ### getAttributeCount

    public int getAttributeCount()
    Gets the number of attributes that are defined.
    Specified by:
      `getAttributeCount` in interface `AttributeSet`
    Returns:
      the number of attributes >= 0
    See Also:
      - `AttributeSet.getAttributeCount()`

  - ### isDefined

    public boolean isDefined(Object attrName)
    Checks whether a given attribute is defined.
    Specified by:
      `isDefined` in interface `AttributeSet`
    Parameters:
      `attrName` - the non-null attribute name
    Returns:
      true if the attribute is defined
    See Also:
      - `AttributeSet.isDefined(Object)`

  - ### isEqual

    public boolean isEqual(AttributeSet attr)
    Checks whether two attribute sets are equal.
    Specified by:
      `isEqual` in interface `AttributeSet`
    Parameters:
      `attr` - the attribute set to check against
    Returns:
      true if the same
    See Also:
      - `AttributeSet.isEqual(AttributeSet)`

  - ### copyAttributes

    public AttributeSet copyAttributes()
    Copies a set of attributes.
    Specified by:
      `copyAttributes` in interface `AttributeSet`
    Returns:
      the copy
    See Also:
      - `AttributeSet.copyAttributes()`

  - ### getAttribute

    public Object getAttribute(Object attrName)
    Gets the value of an attribute.
    Specified by:
      `getAttribute` in interface `AttributeSet`
    Parameters:
      `attrName` - the non-null attribute name
    Returns:
      the attribute value
    See Also:
      - `AttributeSet.getAttribute(Object)`

  - ### getAttributeNames

    public Enumeration<?> getAttributeNames()
    Gets the names of all attributes.
    Specified by:
      `getAttributeNames` in interface `AttributeSet`
    Returns:
      the attribute names as an enumeration
    See Also:
      - `AttributeSet.getAttributeNames()`

  - ### containsAttribute

    public boolean containsAttribute(Object name, Object value)
    Checks whether a given attribute name/value is defined.
    Specified by:
      `containsAttribute` in interface `AttributeSet`
    Parameters:
      `name` - the non-null attribute name
      `value` - the attribute value
    Returns:
      true if the name/value is defined
    See Also:
      - `AttributeSet.containsAttribute(Object, Object)`

  - ### containsAttributes

    public boolean containsAttributes(AttributeSet attrs)
    Checks whether the element contains all the attributes.
    Specified by:
      `containsAttributes` in interface `AttributeSet`
    Parameters:
      `attrs` - the attributes to check
    Returns:
      true if the element contains all the attributes
    See Also:
      - `AttributeSet.containsAttributes(AttributeSet)`

  - ### getResolveParent

    public AttributeSet getResolveParent()
    Gets attributes from the parent. If not overridden, the resolving parent defaults to the parent element.
    Specified by:
      `getResolveParent` in interface `AttributeSet`
    Returns:
      the attributes from the parent
    See Also:
      - `AttributeSet.getResolveParent()`

  - ### addAttribute

    public void addAttribute(Object name, Object value)
    Adds an attribute.
    Specified by:
      `addAttribute` in interface `MutableAttributeSet`
    Parameters:
      `name` - the non-null attribute name
      `value` - the attribute value
    See Also:
      - `MutableAttributeSet.addAttribute(Object, Object)`

  - ### addAttributes

    public void addAttributes(AttributeSet attr)
    Adds a set of attributes to the element.
    Specified by:
      `addAttributes` in interface `MutableAttributeSet`
    Parameters:
      `attr` - the attributes to add
    See Also:
      - `MutableAttributeSet.addAttribute(Object, Object)`

  - ### removeAttribute

    public void removeAttribute(Object name)
    Removes an attribute from the set.
    Specified by:
      `removeAttribute` in interface `MutableAttributeSet`
    Parameters:
      `name` - the non-null attribute name
    See Also:
      - `MutableAttributeSet.removeAttribute(Object)`

  - ### removeAttributes

    public void removeAttributes(Enumeration<?> names)
    Removes a set of attributes for the element.
    Specified by:
      `removeAttributes` in interface `MutableAttributeSet`
    Parameters:
      `names` - the attribute names
    See Also:
      - `MutableAttributeSet.removeAttributes(Enumeration)`

  - ### removeAttributes

    public void removeAttributes(AttributeSet attrs)
    Removes a set of attributes for the element.
    Specified by:
      `removeAttributes` in interface `MutableAttributeSet`
    Parameters:
      `attrs` - the attributes
    See Also:
      - `MutableAttributeSet.removeAttributes(Enumeration)`

  - ### setResolveParent

    public void setResolveParent(AttributeSet parent)
    Sets the resolving parent.
    Specified by:
      `setResolveParent` in interface `MutableAttributeSet`
    Parameters:
      `parent` - the parent, null if none
    See Also:
      - `MutableAttributeSet.setResolveParent(AttributeSet)`