mukesh patel mukesh patel - 2 months ago 6
Java Question

How can we access type and subtypes of enum in java

I need to implement following using enum.

import java.util.*;

/**
*
* @author mukesh
*/
public class MyTest {
private List<SubTypes> subTypes = new ArrayList<SubTypes>();
enum Types {
PercentArea, StackedArea, ChartSubType3;
}

enum SubTypes {
PERSENTAREA1(Types.PercentArea),
PERSENTAREA2(Types.PercentArea),
PERSENTAREA3(Types.PercentArea),
STACKAREA1(Types.StackedArea),
STACKAREA2(Types.StackedArea),
STACKAREA3(Types.StackedArea);



Types t;
private SubTypes(Types t) {
this.t = t;
}
}

public List<SubTypes> getSubTypes() {
return Collections.unmodifiableList(subTypes);
}

public static void main(String[] args) {
/* How can i get SubTypes based on base type ??? */
}}


Example :
suppose in one combo i have Types -PercentArea, StackedArea, ChartSubType3 etc. and if i select PercentArea then second combo can get sub types e.g. PERSENTAREA1,PERSENTAREA2 etc
Can any one guide me how do i can retrieve all the values of subtype for given types.
A little help will guide me to achieve my goal.
Thanks in advanced.

Answer

I think you need something like this

private static void getSubTypes(Types type)
{
    SubTypes[] values = SubTypes.values();
    for(SubTypes value : values)
    {
        if(value.t == type){
            System.out.println("found subtype "+ value);
        }
    }

}

hope this helps!

Good luck!