Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, using one ActionListener for multiple radio buttons

I have six radio buttons in a panel, and I would like to listen for a mouse click on the panel, then determine which radio button is selected, and perform an action accordingly.

But when I set this situation up and tried it, with a break point in the action listener, the code doesn't seem to invoke the action listener at all. Any explanation as to why this is so, or an alternate way to avoid writing action listeners for each button, would be greatly appreciated.

Thanks in advance for any tips.

John Doner

like image 265
John R Doner Avatar asked Dec 23 '10 15:12

John R Doner


2 Answers

This code will loop and programatically add the listneers.

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;

    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;

    public class Test {
        public Test() {
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();
            ButtonGroup bg = new ButtonGroup();

            for (int i = 0; i < 6; i++) {
                JRadioButton rb = new JRadioButton();
                // ID of Button
                rb.setActionCommand("button " + i);

                try {
                    //method to call, after pressed a button
                    Method m = getClass()
                            .getDeclaredMethod("RadioButton" + (i+1), null);
                    ActionListener al = new MyActionListener(m, this);
                    rb.addActionListener(al);
                } catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //              
                bg.add(rb);
                panel.add(rb);
            }

            frame.setContentPane(panel);
            frame.setVisible(true);
            frame.pack();
        }
        /*buttons actions*/
        public void RadioButton1() {
            System.out.println("Boton1");
        }

        public void RadioButton2() {
            System.out.println("Boton2");
        }

        public void RadioButton3() {
            System.out.println("Boton3");
        }

        public void RadioButton4() {
            System.out.println("Boton4");
        }

        public void RadioButton5() {
            System.out.println("Boton5");
        }

        public void RadioButton6() {
            System.out.println("Boton6");
        }

        public static void main(String[] args) {
            new Test();
        }

        class MyActionListener implements ActionListener {
            Method call = null;
            Object object;

            public MyActionListener(Method m, Object value) {
                call = m;
                object = value;
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    //call method
                    call.invoke(object, null);
                } catch (IllegalArgumentException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (InvocationTargetException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
    }
like image 84
jrey Avatar answered Oct 15 '22 00:10

jrey


The radio buttons are swallowing the event and its never making it up to the JPanel. If you want to know which button was pressed you need to add the action listener to each of the buttons. Have a look at the trail on using radio buttons

like image 33
tddmonkey Avatar answered Oct 15 '22 02:10

tddmonkey