Announcement Announcement Module
Collapse
No announcement yet.
Space in enum Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Space in enum

    hi all:
    I tied this

    enum type --class ~.reference.IndianStates
    enum constant --name Andhra Pradesh

    but could see that IndianStates.java had the following entries

    public enum IndianStates {

    Andhra

    }

    anything I am missing/ don't know ?

    I was hoping it would be

    public enum IndianStates {

    Andhra Pradesh

    }

    thanks
    rp

  • #2
    Spaces aren't allowed within enum constants in Java

    Enum constants can't have spaces in them; your expected code wouldn't compile, regardless of whether you're using Roo. This would be OK:

    Code:
    public enum IndianStates {
    
        Andhra_Pradesh  // Note the underscore
    
    }
    Although personally I like to use all upper case for enum constants.

    Comment


    • #3
      Thanks for your reply and pointing out the default behavior of enum.

      I was very keen to know that in Roo - how would anyone go about creating "drop downs" on the default UI it generates.

      One quick way was creating an enum and mapping it back to a domain class like shown in one of the Roo samples

      enum type --class ~.reference.PetType
      enum constant --name Dog
      enum constant --name Cat
      enum constant --name Bird

      and the later . . .

      entity --class ~.domain.Owner --extends ~.domain.AbstractPerson --testAutomatically
      .
      .
      .
      field enum --fieldName specialty --type ~.reference.Specialty --notNull false


      so this a a nice way to quickly create a drop down of pet type.
      what if the pet type had a space ? any thoughts if I could have used something else instead of enum then ?

      thanks

      rohit

      Comment


      • #4
        Override toString()

        The drop-downs just show the toString() of the relevant object, so you can achieve the desired display values by overriding that method per enum and/or per constant as follows:

        Code:
            enum State {
                
                Andhra_Pradesh,    // uses the enum-level toString
                
                Gujarat {
                    @Override
                    public String toString() {
                        return "A totally different name";
                    }
                };
                
                @Override
                public String toString() {
                    return name().replace("_", " ");
                }
            }

        Comment


        • #5
          you are abs rite.
          Thanks for the "core" java help !

          rohit

          Comment

          Working...
          X