Howdy, Stranger!

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

Categories

actionPerformed

MongloidooMongloidoo Member Posts: 52
I created radio buttons and added an actionlistener to each one so when i select the button the image changes on the screen. But no matter what i do i can't seem to get the method to fire each time the button gets selected.

Here is the group of buttons...

JRadioButton arcButton = new JRadioButton("Arc");
arcButton.addActionListener(al);
p.add(arcButton);

JRadioButton ovalButton = new JRadioButton("Oval");
ovalButton.addActionListener(al);
p.add(ovalButton);

JRadioButton polygonButton = new JRadioButton("Polygon");
polygonButton.addActionListener(al);
p.add(polygonButton);

JRadioButton rectButton = new JRadioButton("Rectangle");
rectButton.addActionListener(al);
p.add(rectButton);

JRadioButton roundrectButton = new JRadioButton("Round Rectangle");
roundrectButton.addActionListener(al);
p.add(roundrectButton);

JRadioButton figure8Button = new JRadioButton("Figure 8");
figure8Button.addActionListener(al);
p.add(figure8Button);

JRadioButton starButton = new JRadioButton("Star");
starButton.addActionListener(al);
p.add(starButton);


and here is my actionPerformed method, what should i add to it to get my methods to fire? (the next and previous buttons work fine)

public void actionPerformed(ActionEvent e) {
String arg = e.getActionCommand();

if ("next".equals(arg)) {
((CardLayout)mainPanel.cards.getLayout()).next(mainPanel.cards);
}
else if ("previous".equals(arg)) {
((CardLayout)mainPanel.cards.getLayout()).previous(mainPanel.cards);
}


}

Comments

  • nightsurfernightsurfer Member Posts: 272
    What's al? Try arcButton.addActionListener(this); and then if(evt.getSource() == arcButton) { do something }



    : I created radio buttons and added an actionlistener to each one so when i select the button the image changes on the screen. But no matter what i do i can't seem to get the method to fire each time the button gets selected.
    :
    : Here is the group of buttons...
    :
    : JRadioButton arcButton = new JRadioButton("Arc");
    : arcButton.addActionListener(al);
    : p.add(arcButton);
    :
    : JRadioButton ovalButton = new JRadioButton("Oval");
    : ovalButton.addActionListener(al);
    : p.add(ovalButton);
    :
    : JRadioButton polygonButton = new JRadioButton("Polygon");
    : polygonButton.addActionListener(al);
    : p.add(polygonButton);
    :
    : JRadioButton rectButton = new JRadioButton("Rectangle");
    : rectButton.addActionListener(al);
    : p.add(rectButton);
    :
    : JRadioButton roundrectButton = new JRadioButton("Round Rectangle");
    : roundrectButton.addActionListener(al);
    : p.add(roundrectButton);
    :
    : JRadioButton figure8Button = new JRadioButton("Figure 8");
    : figure8Button.addActionListener(al);
    : p.add(figure8Button);
    :
    : JRadioButton starButton = new JRadioButton("Star");
    : starButton.addActionListener(al);
    : p.add(starButton);
    :
    :
    : and here is my actionPerformed method, what should i add to it to get my methods to fire? (the next and previous buttons work fine)
    :
    : public void actionPerformed(ActionEvent e) {
    : String arg = e.getActionCommand();
    :
    : if ("next".equals(arg)) {
    : ((CardLayout)mainPanel.cards.getLayout()).next(mainPanel.cards);
    : }
    : else if ("previous".equals(arg)) {
    : ((CardLayout)mainPanel.cards.getLayout()).previous(mainPanel.cards);
    : }
    :
    :
    : }
    :

    There are two methods in software design. One is to make the program so simple, there are obviously no errors. The other is to make it so complicated, there are no obvious errors.

  • MongloidooMongloidoo Member Posts: 52
    When i do that i get a syntax error...
    GraphicsPanel.java [62:1] addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (GraphicsPanel) arcButton.addActionListener(this);

    This is my full code for GraphicsPanel.java and GraphicsTest.java...


    import java.awt.*;
    import java.util.*;
    import java.awt.event.*;
    import java.applet.Applet;
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;


    /** Creates The Graphics Panel */
    public class GraphicsPanel extends Panel {

    ActionListener al;
    ItemListener il;
    public GraphicsCards cards;



    /** Sets dimensions of the graphics panel
    * @return
    */
    public Dimension getPreferredSize() {
    return new Dimension(200, 100);
    }

    /** Invoked when an action occurs.
    *
    */
    public void actionPerformed(ActionEvent e) {
    }

    /**
    * @param listener
    */
    GraphicsPanel(EventListener listener) {
    al = (ActionListener)listener;
    il = (ItemListener)listener;

    setLayout(new BorderLayout());

    add("Center", cards = new GraphicsCards());

    Panel p = new Panel();
    //p.setLayout(new BorderLayout());

    Button b = new Button("next");
    b.addActionListener(al);
    p.add(b);

    b = new Button("previous");
    b.addActionListener(al);
    p.add(b);

    p.add(new Label("go to:", Label.RIGHT));

    Choice c = new Choice();
    c.addItemListener(il);
    //p.add(c);


    JRadioButton arcButton = new JRadioButton("Arc");
    arcButton.addActionListener(al);
    p.add(arcButton);

    JRadioButton ovalButton = new JRadioButton("Oval");
    ovalButton.addActionListener(al);
    p.add(ovalButton);

    JRadioButton polygonButton = new JRadioButton("Polygon");
    polygonButton.addActionListener(al);
    p.add(polygonButton);

    JRadioButton rectButton = new JRadioButton("Rectangle");
    rectButton.addActionListener(al);
    p.add(rectButton);

    JRadioButton roundrectButton = new JRadioButton("Round Rectangle");
    roundrectButton.addActionListener(al);
    p.add(roundrectButton);

    JRadioButton figure8Button = new JRadioButton("Figure 8");
    figure8Button.addActionListener(al);
    p.add(figure8Button);

    JRadioButton starButton = new JRadioButton("Star");
    starButton.addActionListener(al);
    p.add(starButton);

    ButtonGroup group = new ButtonGroup();
    group.add(arcButton);
    group.add(ovalButton);
    group.add(polygonButton);
    group.add(rectButton);
    group.add(roundrectButton);
    group.add(figure8Button);
    group.add(starButton);


    add("East", p);//Move Buttons to the Right

    setSize(400, 400);



    Here is GraphicsTest.java....


    import java.awt.*;
    import java.util.*;
    import java.awt.event.*;
    import java.applet.Applet;

    /** Test **/
    public class GraphicsTest extends Applet
    implements ActionListener, ItemListener {
    private GraphicsPanel mainPanel;

    /** Entry point for excution as an applet **/
    public void init() {
    setLayout(new BorderLayout());
    add("Center", mainPanel = new GraphicsPanel(this));
    }

    /** Used to kill applet **/
    public void destroy() {
    remove(mainPanel);
    }



    /** Capture actions for buttons
    * @param e
    **/
    public void actionPerformed(ActionEvent e) {
    String arg = e.getActionCommand();

    if ("next".equals(arg)) {
    ((CardLayout)mainPanel.cards.getLayout()).next(mainPanel.cards);
    }
    else if ("previous".equals(arg)) {
    ((CardLayout)mainPanel.cards.getLayout()).previous(mainPanel.cards);
    }


    }



    /** Change item when the menu state changes
    * @param e
    **/
    public void itemStateChanged(ItemEvent e) {
    ((CardLayout)mainPanel.cards.getLayout()).show(mainPanel.cards,(String)e.getItem());
    }




    /** Label Applet Frame
    * @param args
    **/
    public static void main(String args[]) {
    AppletFrame.startApplet("GraphicsTest", "Graphics Test", args);
    }

    /** Set applet information
    * @return
    **/
    public String getAppletInfo() {
    return "An interactive demonstration of some graphics.";
    }
    }




  • Justin BibJustin Bib USAMember Posts: 0

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

Sign In or Register to comment.