Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listening on ButtonGroup for "child" changes, and print selected JRadioButton's text

What I want to is: Create an event that fires if the a JRadioButton contained in the ButtonGroup is selected, and then print the text there is on the JRadioButton.

like image 832
Tyilo Avatar asked Jul 30 '11 14:07

Tyilo


People also ask

Which listener is used for radio button?

Usually, you handle radio button clicks using an action listener. Below is the code from RadioButtonDemo. java that creates the radio buttons in the previous example and reacts to clicks. For each group of radio buttons, you need to create a ButtonGroup instance and add each radio button to it.

What can you add to a ButtonGroup?

A ButtonGroup can be used with any set of objects that inherit from AbstractButton . Typically a button group contains instances of JRadioButton , JRadioButtonMenuItem , or JToggleButton .

What class displays radio buttons and check boxes?

Checkboxes and radio buttons are represented by instances of JCheckBox and JRadioButton , respectively. Radio buttons can be tethered together using an instance of another class called ButtonGroup .

What is ButtonGroup?

The ButtonGroup component manages the selected/unselected state for a set of buttons. For the group, the ButtonGroup instance guarantees that only one button can be selected at a time. Initially, all buttons managed by a ButtonGroup instance are unselected.


1 Answers

As per my comment, you can't add a listener to a ButtonGroup. You will likely have to go with an ActionListener added to the individual JRadioButtons.

If this doesn't answer your question, please tell us more details about your problem.

Edit 1
I suppose you could always extend ButtonGroup such that it accepts ActionListeners. For example:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.event.EventListenerList;

@SuppressWarnings("serial")
public class MyButtonGroup extends ButtonGroup {
   private ActionListener btnGrpListener = new BtnGrpListener();
   private EventListenerList listenerList = new EventListenerList();

   @Override
   public void add(AbstractButton b) {
      b.addActionListener(btnGrpListener);
      super.add(b);
   }

   public void addActionListener(ActionListener listener) {
      listenerList.add(ActionListener.class, listener);
   }

   public void removeActionListener(ActionListener listener) {
      listenerList.remove(ActionListener.class, listener);
   }

   protected void fireActionListeners() {
      Object[] listeners = listenerList.getListenerList();
      String actionCommand = "";
      ButtonModel model = getSelection();
      if (model != null) {
         actionCommand = model.getActionCommand();
      }
      ActionEvent ae = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand);
      for (int i = listeners.length-2; i>=0; i-=2) {
          if (listeners[i]== ActionListener.class) {
              ((ActionListener)listeners[i+1]).actionPerformed(ae);
          }
      }
   }

   private class BtnGrpListener implements ActionListener {

      public void actionPerformed(ActionEvent ae) {
         fireActionListeners();
      }
   }
}

And tested by the following:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class MyButtonGroupTest {
   private static void createAndShowUI() {
      String[] data = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

      JPanel panel = new JPanel(new GridLayout(0, 1));
      MyButtonGroup myBtnGrp = new MyButtonGroup();
      myBtnGrp.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            System.out.println("Action Command is: " + e.getActionCommand());
         }
      });

      for (String text : data) {
         JRadioButton radioBtn = new JRadioButton(text);
         radioBtn.setActionCommand(text);
         myBtnGrp.add(radioBtn);
         panel.add(radioBtn);
      }

      JFrame frame = new JFrame("MyButtonGroupTest");
      frame.getContentPane().add(panel);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

But this still eventually adds ActionListers to each JRadioButton to achieve its ends; it just does this behind the scenes in the MyButtonGroup's add method override.

like image 101
Hovercraft Full Of Eels Avatar answered Nov 15 '22 17:11

Hovercraft Full Of Eels