• Post Reply Bookmark Topic Watch Topic
  • New Topic

ActionListener

 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an array of buttons called buttonArray that I would like to attach 9 actionListeners to them. Needless to say I am out of ideas. Here is my code:



 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what problem are you facing. In your code you already added an action listener

Moving to the UI forums...
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I did but I get this error

cannot find symbol
symbol: method addActionListener(<anonymous ActionListener>)
location: variable buttonArray of type JButton[]

Confusing indentation
----
(Alt-Enter shows hints)
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan O'Neill wrote:
Confusing indentation

Ah! You mean this block

Hint:Where does your for block start and where does it end? Curly braces?
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well yeah, you can't attach the listener to the array. It doesn't work that way. You have to write some code which loops through the array and attaches the listener to each of the elements of the array, i.e. each of the JButtons separately.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I am really struggling with the actionListener class nothing I do seems to be right. Now that I have the indentation fixed how do I fix this error:
"cannot find symbol
symbol: method addActionListener(<anonymous ActionListener>)
location: variable buttonArray of type JButton[]
----
(Alt-Enter shows hints)"
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Well yeah, you can't attach the listener to the array. It doesn't work that way. You have to write some code which loops through the array and attaches the listener to each of the elements of the array, i.e. each of the JButtons separately.



How do you attach an actionListener to each button?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan O'Neill wrote:
How do you attach an actionListener to each button?


Are you adding the listener to the button or the array? :wink:
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I am attaching it to the array
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan O'Neill wrote:I guess I am attaching it to the array

Well then, just attach it to the button and your problem should be solved!
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got It! Thanks a bunch
Now I just have to figure out how to update the board
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan O'Neill wrote:Got It! Thanks a bunch

You are welcome!

On a related note, you can create one instance of the listener, and share it between the buttons (since the listener functionality is common to all the buttons)
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks
Another question how can I use getSource to determine if the button has been pressed or not?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getSource lets you determined which component was the source which triggered off the action. You need not worry if it was triggered. Your listener gets notified (via actionPeformed) only if someone triggered it.
In other words, if your actionPerformed is being invoked (by the JVM) then you can be assured someone triggered it.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So How can I tell what is on the button?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan O'Neill wrote:So How can I tell what is on the button?

Dont you it already?
From the code you posted
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line doesn't work

Somehow I need to use the buttonsArray to do that
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once the button is clicked which I think I know by the action listener
I need to know how to reference the random ActionListener with buttonArray[i]
and if the text on the button at buttonArray[i] is blank, enter in a loop like this
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why can't you figure out the source inside your
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do have it in that constructor How do I change the text on the button?

This is what I have now and my IDE says cannot locate setText
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure what problems you are facing. You have posted a code snippet which changes the button text and you are asking how to change the button text? Are we having a communication problem perhaps?
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you change the text in the button not in the String.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help I have it printing on the buttons
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!