Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Retrieving Text off of a button

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to check to see if the button has anything on it.
Line 81 says incompatible types: JButton cannot be converted to String ')' expected ';' expected


 
Bartender
Posts: 4007
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ryan,

you seem to be struggling a lot to get the button that has been clicked
upon.

So I like to give you two ways to simplify identifying this button.

My favorite way is to subclass Button, although I usually use JLabels
for such things.
For instance:
Then, in your actionListener, all you need to do is


Of course, in your setup, you need to create these 'MyButton's,
for instance:

Another way is to store the JButton, (r, c) - combination in a
HashMap<JButton, int[]>. In your actionListener, you then
simply say

But back to your question: look at lines 85 and 88.
This is also the way to retrieve the text on a button.

Beware though, that it is easier to use a decent array int[3][3]
as a board, and to use that for the game logic.

Greetz,
Piet
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic