Enum not flexible enough?
I am a little bit frustrated with the new enum statement and Enum class.
It is true it has much more features than the equivalent in other languages, but I feel like
it just lacks something to make it truly great.
Sorry if people already debated the topic before...
My main problem is the following:
"All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else."
I feel like it shouldn't be the case, we should have the possibility to extend manually the Enum class and have enum instantiate our new class.
Let's take for example the following problem: In my swing app, I would like to have a class that basically represent a JButton.
publicclass ButtonEnumextends Enum{
JButton m_button;
public ButtonEnum(String name,int ordinal, String label){
super(name, ordinal);
m_button =new JButton(label);
}
public JButton getButton(){
return m_button;
}
}
Then in other classes I could easily instantiate those ButtonEnum and manipulate them in a very convenient way.
publicclass DevicePanelextends JPanelimplements ActionListener{
// easily create instances
enum<ButtonEnum> Button{
START("Start"),
STOP("Stop");
}
...
// manipulate them
for (Button button : Button.values())
button.getButton().addActionListener(this);
...
// easy action listener
publicvoid actionPerformed(ActionEvent event){
if (event.getSource() == Button.START.getButton()){
...
}
}
}
// Do the same in other classes
publicclass OtherPanelextends JPanelimplements ActionListener{
// easily create instances
enum<ButtonEnum> Button{
EDIT("Edit"),
DELETE("Delete");
}
...
}
This would be even more convenient for a class with a lot of properties and fields. It enables to specify all fields together during instantiation, reduce the line of codes, makes it easy to go through the enums and test them.
Basically the enum statement would have a "generics" to choose which class you want as a base. That class would have to implement an "Enum interface", which can be easily done by extending Enum.
The "enum<ButtonEnum> Button" statement would automatically create the Button class that extends ButtonEnum instead of Enum, and then add the static fields. The only trick is the constructor, which should automatically be bound to the ButtonEnum's one.
Just a thought...
Maybe there is a solution to do all that with the current features?
Basically I want to be able to:
- create a bunch of JButtons, if possible with one line of code for each.
- Be able to iterate through them with a loop
- Be able to test a JButton the same way as "if (event.getSource() == m_StartButton)"
- All of this is possible with a local Enum, but I would like to use this in several classes that use different buttons.
The best way I can see is to:
// Create an enum for each button
enum Button{
START,
STOP;
}
// have a seperate array of JButton
JButton[] m_buttons;
// then instantiate in the constructor
public DevicePanel(){
m_buttons =new JButton[Button.values().length];
m_buttons[Button.START.ordinal()] =new JButton("Start");
m_buttons[Button.STOP.ordinal()] =new JButton("Stop");
}
// then you can iterate and test
...
This is much heavier than the previous solution...
What do you guys think? Thank you for your advice
AJG

