Jump to content
  • Advertisement
Sign in to follow this  
econobeing

How would I switch an ActionEvent in java?

This topic is 3262 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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;
}
}

Share this post


Link to post
Share on other sites
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));

Share this post


Link to post
Share on other sites
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;
}
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!