MutableAttributeSet Isn't

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SammyB
    Recognized Expert Contributor
    • Mar 2007
    • 807

    #1

    MutableAttributeSet Isn't

    Where do they dream up these object names? Never mind...

    We had a program where we used a bunch of styles in a Document in a JTextPane. Everything worked fine until they wanted us to make a modification to use two different fonts. The original code that they provided was something like:
    [code=java]
    protected void setStyles(JText Pane textPane)
    {
    Style SansSerifFontSt yle =
    StyleContext.ge tDefaultStyleCo ntext().getStyl e
    (StyleContext.D EFAULT_STYLE);
    StyleConstants. setFontFamily(S ansSerifFontSty le, "SansSerif" );
    Style SansSerifRegula rStyle =
    textPane.addSty le("regular", SansSerifFontSt yle);
    }
    [/code]

    (the actual code also had an italic style, etc.)

    So, I changed it to
    [code=java]
    protected void setStyles(JText Pane textPane)
    {
    //Serif Font Style
    Style SansSerifFontSt yle =
    StyleContext.ge tDefaultStyleCo ntext().getStyl e
    (StyleContext.D EFAULT_STYLE);
    StyleConstants. setFontFamily(S ansSerifFontSty le, "SansSerif" );
    Style SansSerifRegula rStyle =
    textPane.addSty le("SansSerif-regular", SansSerifFontSt yle);
    //Comic Font Style
    Style comicFontStyle =
    StyleContext.ge tDefaultStyleCo ntext().getStyl e
    (StyleContext.D EFAULT_STYLE);
    StyleConstants. setFontFamily(c omicFontStyle, "Comic Sans MS");
    Style comicRegularSty le =
    textPane.addSty le("Comic-regular", comicFontStyle) ;
    }
    [/code]

    Then in my code I just prefixed the Document style name with Comic or SansSerif. Initially everything should have been SansSerif, but is was Comic. Looking at the documentation for the methods that I used, it looks like I probably just gave the same object two different names, but I'm a little unclear how to produce two different objects based on the DefaultStyleCon text. TIA --Sam
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by SammyB
    Where do they dream up these object names? Never mind...
    You ain't seen nothing yet; wait 'till you meet the Spring framework ;-) Seriously
    though, are they teaching you *that* on your Java course? I once had to figure
    that complicated stuff out (only once) so I have to dig in my 'archives' (read: mess).
    As far as I can see from that code blob: you're putting both styles in the same
    context and the last one wins. Allow me to check it out when I find some time.

    kind regards,

    Jos

    Comment

    Working...