Radio button and JButton problem
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
emint
Junior Poster in Training
60 posts since Jun 2009
Reputation Points: 10
Solved Threads: 4
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.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
thank you for ur reply, how will i do this?
emint
Junior Poster in Training
60 posts since Jun 2009
Reputation Points: 10
Solved Threads: 4
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.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
i dun know. but i know how to use extends keyword. thx
emint
Junior Poster in Training
60 posts since Jun 2009
Reputation Points: 10
Solved Threads: 4
Why the map? Why not just test the button to see if it's checked?
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Ok how can i test radio button is checked or not, i tried but did not work. pls help meh
emint
Junior Poster in Training
60 posts since Jun 2009
Reputation Points: 10
Solved Threads: 4
if (myRadioButton.isSelected()) { ...
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
thank you so much, u r realy great. it worked thank you
emint
Junior Poster in Training
60 posts since Jun 2009
Reputation Points: 10
Solved Threads: 4
JamesCherrill thank you for ur effective n short solution. thank u
emint
Junior Poster in Training
60 posts since Jun 2009
Reputation Points: 10
Solved Threads: 4
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?
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
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?
emint
Junior Poster in Training
60 posts since Jun 2009
Reputation Points: 10
Solved Threads: 4
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.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073