New to Java, I'm looking for a general way to set up a game board as a
grid of cells and communicate with them. So far I have defined a new
class, Cell, which extends JTextField and has some extra fields like
rowNo and columnNo, then populated a JPanel with a GridLayout(3,3)
with nine instances of Cell(i,j).
The problem then is how to code an event handler (like
KeyTyped(KeyEvent e)) in the Class to know which instance of Cell has
received the event.
The closest I have got so far finding ActionCommand and defining that
to be a row+column id string for the instance as it is constructed.
This is then visible in a System.out.println of the Event but I have
not found a way to extract it. I'm sure that is an abuse of
ActionCommand anyway.
Any and all ideas on better/correct techniques will be most welcome.
Joshua Cranmer - 16 Sep 2007 22:53 GMT
> New to Java, I'm looking for a general way to set up a game board as a
> grid of cells and communicate with them. So far I have defined a new
[quoted text clipped - 13 lines]
>
> Any and all ideas on better/correct techniques will be most welcome.
Try adding a KeyListener to every Cell object, which should know its
position.

Signature
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
Knute Johnson - 17 Sep 2007 00:48 GMT
> New to Java, I'm looking for a general way to set up a game board as a
> grid of cells and communicate with them. So far I have defined a new
[quoted text clipped - 13 lines]
>
> Any and all ideas on better/correct techniques will be most welcome.
In your KeyListener, get the source of the event, your Cell component
and take the row and column from that.
public void keyPressed(KeyEvent ke) {
Cell cell = (Cell)ke.getSource();
// then you have cell.row or cell.getRow() whatever

Signature
Knute Johnson
email s/nospam/knute/
jim.hunt533@btinternet.com - 17 Sep 2007 14:37 GMT
On Sep 17, 12:48 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> jim.hunt...@btinternet.com wrote:
> > New to Java, I'm looking for a general way to set up a game board as a
[quoted text clipped - 28 lines]
>
> - Show quoted text -
That works! That's great - many thanks - Jim
Roedy Green - 17 Sep 2007 09:36 GMT
>The problem then is how to code an event handler (like
>KeyTyped(KeyEvent e)) in the Class to know which instance of Cell has
>received the event.
You can use a Canvas, then analyse the x,y (just divide by a constant)
to get the cell number. This will be considerably faster than having
a layout of components.
See http://mindprod.com/jgloss/event11.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
jim.hunt533@btinternet.com - 17 Sep 2007 14:39 GMT
On Sep 17, 9:36 am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Sun, 16 Sep 2007 13:49:35 -0700, jim.hunt...@btinternet.com wrote,
> quoted or indirectly quoted someone who said :
[quoted text clipped - 11 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com
Thanks Roedy - I'll keep an eye on performance and maybe go to this
technique if necessary - Jim