Common action listener for 3 buttons

I am having problems designing my code. I have three buttons not in the button group. I want - based on the selected button - to perform an action. Now the action requires modifying the object in the class. This means that I cannot use the inner class because it does not have access to the outer. If I could add an event listener to the button group, that would be much simpler, but as I see it, I need an event handler for each switch, is that correct? If not, how else can I do this? Thanks

Quick example

public class Test(){ RadioButton 1 = new RadoButton(); RadioButton 2 = new RadoButton(); RadioButton 3 = new RadoButton(); Object myObject = new Object(); public void clickEvent(){ if(1.isSelected()){ myObject.doOne(); }else if(2.isSelected()){ myObject.doTwo(); }..... } } 
+4
source share
2 answers

You can set the same listener on all of your buttons.

Pseudocode:

 radioButton1 = new RadioButton(); radioButton2 = new RadioButton(); radioButton3 = new RadioButton(); listener = new ActionListener() { ... } radioButton1.addActionListener(listener); radioButton2.addActionListener(listener); radioButton3.addActionListener(listener); 
+2
source

This shows how you can use the inner class :

 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JRadioButton; public class TestInnerClass { JRadioButton radioOne = new JRadioButton(); JRadioButton radioTwo = new JRadioButton(); JRadioButton radioThree = new JRadioButton(); Object myObject = new Object(); public TestInnerClass() { ActionListener myInnerClass = new MyActionListener(); radioOne.addActionListener(myInnerClass); radioTwo.addActionListener(myInnerClass); radioThree.addActionListener(myInnerClass); } private class MyActionListener implements ActionListener { @Override public void actionPerformed(ActionEvent event) { if(radioOne.isSelected()) myObject.toString(); else if(radioTwo.isSelected()) myObject.notify(); else if(radioThree.isSelected()) myObject.getClass().getName(); } } } 
  • Note that the inner class is not static, as stated in the gontard comment, therefore it is visible to myObject. And it’s safer to keep it secret.

It is often useful for one listener to handle all events, as in this case. However, there are other cases where you want your event handling to be more specific to each component. For example, in these cases, radioThree may trigger an event, and since these buttons are not part of a group, it is possible that RadioOne is still in the selected state. This single handler will fire and act only on the first radio. Although one way to fix this would be to add checks for the source, as in:

 public void actionPerformed(ActionEvent event) { if(event.getSource() == radioOne && radioOne.isSelected()) myObject.toString(); if(event.getSource() == radioTwo && radioTwo.isSelected()) myObject.notify(); if(event.getSource() == radioThree && radioThree.isSelected()) myObject.getClass().getName(); } 

Another way is to use one listener for each component. That the anonymous class is very convenient:

 radioOne.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { myObject.doOne(); } }); 

One of my favorite patterns, especially if the work is nontrivial, is to first create a method to do the work, and then call it from the listener. I also end the call in SwingUtilities.invokeLater () to get work from the Swing event stream.

 public class Test { JRadioButton radioOne = new JRadioButton(); Object myObject = new Object(); private void handleRadioOne() { myObject.toString(); // etc, etc. } public Test() { radioOne.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { handleRadioOne(); } }); } }); } } 

This provides two nice features:

  • It encapsulates your work in a method that allows programmatic access, if desired later
  • It ensures that this method will work from the Swing event flow, so your GUI will not freeze during heavy processing.
+2
source

Source: https://habr.com/ru/post/1468986/


All Articles