Buttons, Please help!!

I have these radio buttons, each button is supposed to display a different picture. These pictures are all in seperate methods (ovalShape, rectShape, roundRectShape, etc.) How do i make it so that each time i click a radio button, that image is displayed (for example i click on the rectButton so the rectShape method is fired and the rectangle is displayed on the panel i created) I added actionlisteners to each button but i just can't figure out how to use it. I assume i need to have some sort of if-then statement but i am so lost, i would appreciate any help you all could give me.



  • Hi.

    I'd suggest you use only one ActionListener for all the radiobuttons. Then you just distinguish between the different actioncommands you get from the ActionEvent-object. Here's a short example how it could work:
    class Test {
    JRadioButton b1 = new JRadioButton("radio1");
    JRadioButton b2 = new JRadioButton("radio2");
    ActionListener al = new RadioListener();
    class RadioListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();
    if (cmd.equals("radio1")) methodForRadio1();
    else if (cmd.equlas("radio2")) methodForRadio2();
    // and so on - you could use switch for this as well
    void methodForRadio1() {
    // do what has to be done here

    void methodForRadio2() {
    // do what you want to do here

    } // end of class Test

    -- planning is the substitution of hazard by error --

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion