Hi
how can i execute a statement if only radio button is checked and Jbutton is click? i know how to use them Individually. i try to use like this

public void actionPerformed(ActionEvent event)
     {
       Object src = event.getSource();
   if (src==radio1 && src == buttonclick)
     JOptionPane.showMessageDialog(null,"Raido1 is checked");
}

but it did not work. pls help thx

Don't associate an action event with the radio button, but, rather, just with the jbutton, then have the listener simply check the value of the radio button. You will probably have to build up a map with the button->radio associations (or make your own "extneds JButton" class where you can store a reference to the radio button directly), however.

actionPerformed
  if (source == button)
    // if a hashmap
    radio = map.get(button)
    // if a new object
    radio = button.radio

  if (radio.isSelected())

I assume you know how to create a map? I assume you know how to use the "extends" keyword.

i dun know. but i know how to use extends keyword. thx

Ok how can i test radio button is checked or not, i tried but did not work. pls help meh

yep ,

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

        if(jRadioButton1.isSelected())
        {
            // do the operation 
        }   
        else
        {
            //do nothing 
        } 
      
    }

thank you so much, u r realy great. it worked thank you

JamesCherrill thank you for ur effective n short solution. thank u

Why the map? Why not just test the button to see if it's checked?

Assuming there are (or may be) multiple buttons and multiple radio buttons. Why have fifteen anonymous listener classes when a single instance would be more than enough?

Or, why have fifteen if statements when a single map lookup would handle it?

Assuming there are (or may be) multiple buttons and multiple radio buttons. Why have fifteen anonymous listener classes when a single instance would be more than enough?

Or, why have fifteen if statements when a single map lookup would handle it?

you are rite but how would u do it? is it very long n complecated?

Assuming there are (or may be) multiple buttons and multiple radio buttons. Why have fifteen anonymous listener classes when a single instance would be more than enough?

Or, why have fifteen if statements when a single map lookup would handle it?

Thanks for the clarification masijade. I agree that if there are a lot of these things it's worth finding some generic approach rather than repeating almost-identical blocks of code.

I'm very fond of using a ButtonGroup in this kind of situation. You can get an action name easily with
myButtonGroup.getSelection().getActionCommand()

For more cunning solutions I really like using the much-underrated putClientProperty/getClientProperty to associate whatever I want with any particular button - this is probably very close to the way you suggested using the map.

#
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
#

#
if(jRadioButton1.isSelected())
#
{
#
// do the operation
#
}
#
else
#
{
#
//do nothing
#
}
#

#
}

i can't understand this pls explain it how it will be......

This article has been dead for over six months. Start a new discussion instead.