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...
Type: Posts; User: Norm
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...
What is the code in the action listener supposed to do?
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.
@Override /**HandleItemEvent*/
public void actionPerformed(ActionEvent e)
{
// <<<<<<<<<<< THE PRINTLN CODE GOES HERE <<<<<<<<
...
Post the code so I can see what the problem is.
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 );
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...
Post the code you tried and the error message you get from it.
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. ...
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.
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...
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...
That's strange, I see btBody define here:
for (int iCol = 1; iCol<4; iCol++)
{
num = 3*(iRow-1) + iCol;
JButton btBody = new...
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...
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.
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:...
Post#9 gives an example. Something like this:
theButtonReference.addActionListener(theListenerReference);
Your button object reference variable is: btBody
You haven't defined an...
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.
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.
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...
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...
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...