Search:

Type: Posts; User: Norm

Search: Search took 0.09 seconds.

  1. Re: how do I add Action Listener to my code to make the calculator work

    The ActionEvent object passed to the actionlistener method contains a reference to the button that was pressed. Use the event class's getSource() method to get a reference to that button.
    When you...
  2. Re: how do I add Action Listener to my code to make the calculator work

    What is the code in the action listener supposed to do?
  3. Re: how do I add Action Listener to my code to make the calculator work

    To see what is passed to the listener method, print the value of the ActionEvent object: e


    I'm done for tonight. I'll be back tomorrow.
  4. Re: how do I add Action Listener to my code to make the calculator work

    @Override /**HandleItemEvent*/
    public void actionPerformed(ActionEvent e)
    {
    // <<<<<<<<<<< THE PRINTLN CODE GOES HERE <<<<<<<<

    ...
  5. Re: how do I add Action Listener to my code to make the calculator work

    Post the code so I can see what the problem is.
  6. Re: how do I add Action Listener to my code to make the calculator work

    Strange you don't know how to code a println statement. Here is an example:

    System.out.println( PUT THE STRINGS, ETC HERE THAT YOU WANT PRINTED );
  7. Re: how do I add Action Listener to my code to make the calculator work

    The action listener could be called, but there is no code in the actionlistener method. It is empty.
    Add a call to println() that prints out the value of the ActionEvent object (e) that is passed to...
  8. Re: how do I add Action Listener to my code to make the calculator work

    Post the code you tried and the error message you get from it.
  9. Re: how do I add Action Listener to my code to make the calculator work

    Did you add the println() I suggested? What it prints will show the number that is on the face of the button. There is a JButton class method that will return the String that is shown on the button. ...
  10. Re: how do I add Action Listener to my code to make the calculator work

    What happens when you press a button?

    What is the code supposed to do when a button is pressed? When you have decided on what is supposed to happen then work on the code for it.
  11. Re: how do I add Action Listener to my code to make the calculator work

    Move the code you added that adds a listener to btBody to inside the loop where the btBody objects are created.
    For testing add a println() statement inside of the listener method that prints out...
  12. Re: how do I add Action Listener to my code to make the calculator work

    The call to the btBody's addActionListener method must be made where the btBody variable is in scope.
    In scope means within the same enclosing {}s. A variable defined inside of a {} pair is NOT in...
  13. Re: how do I add Action Listener to my code to make the calculator work

    That's strange, I see btBody define here:

    for (int iCol = 1; iCol<4; iCol++)
    {
    num = 3*(iRow-1) + iCol;

    JButton btBody = new...
  14. Re: how do I add Action Listener to my code to make the calculator work

    Did you do the search I recommended? Did you find some examples of how to call the addActionListener() method? Did you try to copy any of the code into your program?
    Do you have any questions...
  15. Re: how do I add Action Listener to my code to make the calculator work

    If you need some examples of action listener code, use the forum's Search for addActionListener. There are lots of code examples on threads in the forum.
  16. Re: how do I add Action Listener to my code to make the calculator work

    You can create an instance of a class by using a new statement. This creates an instance of a JButton:


    JButton btBody = new JButton("" + num);

    Look at the tutorial:...
  17. Re: how do I add Action Listener to my code to make the calculator work

    Post#9 gives an example. Something like this:

    theButtonReference.addActionListener(theListenerReference);

    Your button object reference variable is: btBody
    You haven't defined an...
  18. Re: how do I add Action Listener to my code to make the calculator work

    Another option with the current code is to use one of the JButton class methods to get the String that is being shown by the button and use that to make decisions in the action listener method.
  19. Re: how do I add Action Listener to my code to make the calculator work

    Put the following before the code:



    put the following after the code:


    Use the Preview Changes button in the Go Advanced area to see what happens to the post before saving.
  20. Re: how do I add Action Listener to my code to make the calculator work

    There are a couple of approaches:
    1)Write an anonymous inner class that implements ActionListener for each button
    2)Write one actionPerformed() method and use the Event class's getSource() method...
  21. Re: how do I add Action Listener to my code to make the calculator work

    There are several steps needed.
    1) create a class that implements the ActionListener interface
    2) create an instance of the above class
    3) use the addActionListener() method to add that instance...
  22. Re: how do I add Action Listener to my code to make the calculator work

    Please edit your post and wrap your code with code tags:


    YOUR CODE HERE

    to get highlighting and preserve formatting.

    See the tutorial: How to Write an Action Listener (The Java™ Tutorials...
Results 1 to 22 of 22