0

I have a array of Jradiobuttons.i am trying to have java anonymous class that implement ActionListener so when the user press on a radio button I can do something but since this is an array i cant give the array index using a while loop so how to identify what Jradiobutton I am using.and I want to get the text of that radio button and save it in a another variable...How can I do this?

This is what so far I have done :

if(count!=0) {
   rs=pst.executeQuery();
   JRadioButton a []=new JRadioButton[count];                       
   jPanel3.setLayout(new GridLayout());
   int x=0;
   ButtonGroup bg=new ButtonGroup();

   while(rs.next()) {    
     a[x]=new JRadioButton(rs.getString("name"));
     bg.add(a[x]);
     jPanel3.add(a[x]); 
     a[x].setVisible(true);

     a[x].addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {      

            JOptionPane.showMessageDialog(null,a[x].getText()); //here i cant use this x...even though i make x global value of x always will be 6 becouse of while loop.

        }
     });                  
     x++;
   }                            
}      

2 Answers 2

1

If i understand you correctly, You can set the name of radio button:

a[x]=new JRadioButton(rs.getString("name"));
a[x].setName(rs.getString("name"));

and in ActionPerformed you get the source of action:

public void actionPerformed(ActionEvent e) {

if( e.getSource() instanceof JRadioButton){

  String selectedRadioName = ((JRadioButton) e.getSource()).getName();

  JOptionPane.showMessageDialog( null, selectedRadioName );

}
2
  • and also can you tell me what is this instanceof JRadioButton means
    – pavithra
    Jul 22, 2015 at 6:03
  • Since your ActionListener in implemented as anonymius class so you dont need it. It is to check/confirm and process if you have clicked an JRadioButton (i.e. source of action is radio button) so that you shouldnt get class cast exception on type casting.
    – Garry
    Jul 22, 2015 at 6:11
0

You could...

Supply each JRadioButton with a ActionCommand which will be made available via the ActionEvent

a[x]=new JRadioButton(rs.getString("name"));
a[x].setActionCommand(String.valueOf(x));
//...
a[x].addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            String cmd = e.getActionCommand();
            int value = Integer.parseInt(cmd);

            JOptionPane.showMessageDialog(null, a[value].getText());
        }
}); 

See How to Use Buttons, Check Boxes, and Radio Buttons for more details

You could...

Use the Action API to surround the message and action in a self contained unit of work...

public class MessageAction extends AbstractAction {

    private String message;

    public MessageAction(String text, String message) {
        this.message = message;
        putValue(NAME, text);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(null, message);
    }

}

And then apply it to your button something like...

a[x] = new JRadioButton(new MessageAction(rs.getString("name"), "Hello from " + x);

See How to Use Actions for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.