Howdy, Stranger!

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


Buttons, Please help!!

MongloidooMongloidoo Member Posts: 52
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.



  • KyonKyon Member Posts: 113

    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 --

  • Justin BibJustin Bib USAMember Posts: 0

    ______ || || free ebooks and video tutorials about | MATLAB R Scratch Python Assembly Visual Basic PL/SQL Java JavaScript Ruby C# C++ Visual Basic .NET Delphi C PHP Perl Swift Go Objective-C Hack Apex Erlang Prolog Ada Awk Rust Lisp ML Transact-SQL Alice Crystal Clojure LabVIEW F# VBScript Kotlin Lua Logo SAS D Bash ABAP Scala Dart Julia Scheme COBOL Fortran FoxPro | __

Sign In or Register to comment.