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

    Default Font style constants to string is it possible?

    The Font style constants are of int:
    int[] fontStyleList = new int[] {Font.PLAIN, Font.BOLD, Font.ITALIC};

    But what do I do when I want to add the names such as PLAIN etc. as the name of a menu item?
    The following is not going to work, right!
    String[] parts = (fontStyleList[i].toString()).split("\\."); or String[] parts = ("" + fontStyleList[i]).split("\\.");
    rbStyle = new JRadioButtonMenuItem(parts[1]);

    Is there another way to do this or should I just do another array with the names as a String?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Font style constants to string is it possible?

    Either you have an array with thos names or you could use an enum to match the the int constant with the name.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    willemjav is online now Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: Font style constants to string is it possible?

    I did the following (it works but it is to complicated?)

    1) created an inner class at the font class:

    Java Code:
     public class FontStyleKeyValue {
            private String key;
            private int value;
            
            public FontStyleKeyValue(String key, int value) {
                this.key = key;
                this.value = value;
            }
            
            public String getFontStyleKey() {
                return key;
            }
            
            public int getFontStyleValue() {
                return value;
            }
            
        }
    and created an array of that class:

    Java Code:
    public FontStyleKeyValue[] getAllFontStyles() {
            fontstyleKeyValue = new FontStyleKeyValue[] {
                new FontStyleKeyValue("Regular", Font.PLAIN),
                new FontStyleKeyValue("Bold", Font.BOLD),
                new FontStyleKeyValue("Italic", Font.ITALIC)};
            return fontstyleKeyValue;
            
        }
    at the font menu class I initiated the font class:

    fontclass = new Fontclass();

    and got the array like this:

    FontStyleKeyValue[] fontStyleList = fontclass.getAllFontStyles();

    So now I can ask like this:

    rbStyle = new JRadioButtonMenuItem(fontStyleList[i].getFontStyleKey());

    Font ft = jm.getFont();
    font = new Font("" + ft, fontStyleList[i].getFontStyleValue(), 16);

    I'll look into enum now, thanks

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

    Default Re: Font style constants to string is it possible?

    This enum concept is new to me and kind of confusing.
    I figured out some and when importing the "class" (is it a class?)
    I could do this:

    String s = FontStyle.BOLD.getFontStyleKey();
    int i = FontStyle.PLAIN.getFontStyleValue();
    String s2 = FontStyle.ITALIC.getFontStyleKey();

    Java Code:
    public enum FontStyle {
            PLAIN("Regular", Font.PLAIN),
            BOLD("Bold", Font.BOLD),
            ITALIC("Italic", Font.ITALIC);
            private String key;
            private int value;
            FontStyle (String key, int value)  {
                this.key = key;
                this.value = value;
            }
            
            public int getFontStyleValue()  {
                return value;
            }
            
            public String getFontStyleKey() {
                return key;
            }
        }
    weird it does not need to be initiated?

    Could I put FontStyle.BOLD into a array (PLAIN, BOLD, ITALIC)
    An array of what FontStyle[] or enum[]....
    Last edited by willemjav; 01-20-2014 at 12:17 PM.

Similar Threads

  1. Replies: 5
    Last Post: 04-29-2013, 11:04 AM
  2. Enumerated constants
    By ohbugger in forum Advanced Java
    Replies: 10
    Last Post: 01-29-2011, 01:50 AM
  3. Replies: 0
    Last Post: 08-18-2010, 10:31 AM
  4. Font.PLAIN - won't set font style to PLAIN!
    By Unsub in forum New To Java
    Replies: 3
    Last Post: 03-11-2010, 12:13 PM
  5. Replies: 5
    Last Post: 04-25-2009, 08:00 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
  •