How would I switch an ActionEvent in java?

Started by
2 comments, last by Fenrisulvur 14 years, 6 months ago
Yeah, homework... Apparently I have to decide what button was hit by using a switch statement. But eclipse says that switch only takes ints and enums. How would I about getting either one of those from an ActionEvent? I was going to just switch ActionEvent.getSource(), but no dice, it's not an int or enum.
Advertisement
Are you sure you are reading the question correctly?

I see nothing useful in the ActionEvent class that you could switch on.

Maybe the right thing to do with this is to report that it is impossible to complete the program to the letter. I would have a working program without the switch if I wanted to go down this route though.

Or you can be a smartass:
void function(ActionEvent e){    switch(42)    {    default:        // use an if statement ;)        break;    }}
Use different instance of action listener for button perhaps?

After all, this is what polymorphic switch is.

class TextSetter implements ActionListener {  private final JTextField target;  TextSetter(JTextField target) {    this.target = target;  }  void actionPerformed(ActionEvent e) {    target.setText("Boo");  }};JTextField field1;JTextField field2;JButton button1;JButton button2;button1.addActionListener(new TextSetter(field1));button2.addActionListener(new TextSetter(field2));
Well, this is all I could think of, and you're only really relegating sequential checking of buttons to ArrayList's indexOf() member. Pretty pointless.

class MultiListener implements ActionListener{    private ArrayList<JButton> btns;    public MultiListener()    {        btns = new ArrayList<JButton>();    }    public void registerButtons(JButton btn0,JButton btn1,JButton btn2)    {        btns.add(btn0);        btns.add(btn1);        btns.add(btn2);    }    public void actionPerformed(ActionEvent evt)    {        switch (btns.indexOf(evt.getSource()))        {            case 0:                break;            case 1:                break;            case 2:                break;        }    }}


I suppose if you were okay with extending JButton, et al, with an enumerated or integer ID member, you could do something like this:

class MButton extends JButton{    enum ID { submit, cancel, incompatible };    private ID id;    public MButton(ID p_id, String text)    {        super(text);        id = p_id;    }    public ID getID()    {        return id;    }}class MultiListener2 implements ActionListener{    public void actionPerformed(ActionEvent evt)    {        MButton.ID id = null;        try        {            id = ((MButton)evt.getSource()).getID();        }        catch(Exception e)        {            id = MButton.ID.incompatible;        }        switch (id)        {            case submit:                JOptionPane.showMessageDialog(null,"Submit clicked","Info",                        JOptionPane.INFORMATION_MESSAGE);                break;            case cancel:                JOptionPane.showMessageDialog(null,"Cancel clicked","Info",                        JOptionPane.INFORMATION_MESSAGE);                break;            default:                JOptionPane.showMessageDialog(null,"Component not compatible " +                        "with MultiListener2","Warning",                        JOptionPane.WARNING_MESSAGE);                break;        }    }}

This topic is closed to new replies.

Advertisement