Results 1 to 5 of 5
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default the StyleConstants class and its methods

    Ok one creates a basic style (called Basic) as a root and one adds some stuff at that parent, right!
    -root: basicStyle = doc.addStyle(Basic, null);
    -Some additional leafs (of Style s) with names like Heading, BoldText etc. are added to Basic the root, right?
    My question is about StyleConstants. etc. StyleConstants are inner classes that hold data during the program runs, right? How does, for instance, the StyleConstants.setBold(s, true); method (line 15) knows that it belongs to Headings leaf?



    Java Code:
     protected void createStyles() {
            StyledDocument doc = getStyledDocument();
    
            // Create the top-level style, with the required font
            basicStyle = doc.addStyle(Basic, null);
            StyleConstants.setFontFamily(basicStyle, "SansSerif");
            StyleConstants.setFontSize(basicStyle, 14);
            StyleConstants.setForeground(basicStyle, Color.white);
            StyleConstants.setFirstLineIndent(basicStyle, 50.0f);
            StyleConstants.setSpaceAbove(basicStyle, 6);
            StyleConstants.setSpaceBelow(basicStyle, 0);
    
            // Heading: centered, bold, larger font
            Style s = doc.addStyle(Heading, basicStyle);
            StyleConstants.setBold(s, true);
            StyleConstants.setFontSize(s, 24);
            StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
            StyleConstants.setSpaceAbove(s, 8);
            StyleConstants.setSpaceBelow(s, 12);
    
            // BoldText
            s = doc.addStyle(BoldText, basicStyle);
            StyleConstants.setBold(s, true);
    
            // Cite - a paragraph style for quotations
            s = doc.addStyle(Cite, basicStyle);
            StyleConstants.setItalic(s, true);
            StyleConstants.setFontSize(s, 14);
            StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
            StyleConstants.setSpaceAbove(s, 10);
            StyleConstants.setSpaceBelow(s, 10);
            StyleConstants.setForeground(s, Color.yellow);

  2. #2
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: the StyleConstants class and its methods

    Next code would create a default style for a document (basicStyle). The second argument is null since basicStyle will be the root, right! Other style elements could simply expand on basisStyle like the next code shows, right?
    But where to find the (static?) inner class StyleConstants?

    Java Code:
    StyleContext sc= new StyleContext(); 
    Style basicStyle= sc.addStyle("Basic Style", null);
    basicStyle.addAttribute(StyleConstants.FontFamily, "SansSerif");
    basicStyle.addAttribute(StyleConstants.FontSize, new Integer(12));
    basicStyle.addAttribute(StyleConstants.setForeground, Color.black);

    Java Code:
    Style chapterHeading = sc.addStyle("Chapter Heading", basicStyle);
    chapterHeading.addAttribute(StyleConstants.FontSize , new Integer(16));
    chapterHeading.addAttribute(StyleConstants.Bold, Boolean.TRUE);
    chapterHeading.addAttribute(StyleConstants.SpaceBelow, new Float(12.0));

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: the StyleConstants class and its methods

    Quote Originally Posted by willemjav View Post
    My question is about StyleConstants. etc. StyleConstants are inner classes that hold data during the program runs, right? How does, for instance, the StyleConstants.setBold(s, true); method (line 15) knows that it belongs to Headings leaf?
    It knows that because you're passing the Heading as the first parameter; that's the AttributeSet on which the style is applied.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: the StyleConstants class and its methods

    Ok thanks that is clear to me.
    But where is this class sited?
    I cannot find that fact in the API
    I mean it is not a inner class of the Style class for instance?

    And yet another "stupid" question:
    is parent the same as branch
    and child the same a leaf?
    Last edited by willemjav; 10-27-2013 at 09:09 AM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: the StyleConstants class and its methods

    Quote Originally Posted by willemjav View Post
    Ok thanks that is clear to me.
    But where is this class sited?
    I cannot find that fact in the API
    I mean it is not a inner class of the Style class for instance?

    And yet another "stupid" question:
    is parent the same as branch
    and child the same a leaf?
    A Style is an interface and it has a super interface: the AttributeSet interface and that's what your StyleConstant needs ... A parent is just the parent of a child and (vice versa) a child is a child of a parent.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Access to Class Methods
    By ShadowWalker in forum New To Java
    Replies: 3
    Last Post: 10-23-2013, 02:15 PM
  2. running class methods
    By droidus in forum New To Java
    Replies: 6
    Last Post: 12-10-2012, 12:13 AM
  3. A class calling methods from external class
    By Aaron5466 in forum New To Java
    Replies: 4
    Last Post: 04-16-2012, 11:36 AM
  4. Problem using methods from other class
    By higashi in forum New To Java
    Replies: 5
    Last Post: 10-13-2011, 09:35 AM
  5. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •