I am attempting to make a TicTacToe in java using Swing. Instead of using buttons for mark(x and o) placing on 3x3 grid, i used textfield(variable name- pos) to specify position of mark entering and another textfield(var name- val) to take value to insert it into the position which i got from the first textfield.
The problem i am facing is that when i write

pos.getText();

i get a string value from it. But how do i get actual position name from string and set the "val" in it??
eg:- if a position on the field is a1, then we write a1 in pos textField. But if i getText from pos, how do i get a1 as in its control name in swing and not the actual string.
I hope i am clear.
Pls help.

i think you have to use

pos.getX(),pos.getY() 

methods for the where it is located
(or)

pos.getLocation()

check it once for your desired outout
and let me know the status after changing your code

i think you have to use

pos.getX(),pos.getY()
methods for the where it is located
(or)

pos.getLocation()
check it once for your desired outout
and let me know the status after changing your code

The OP doesn't want to get the location/position of the textfield. He wants to create a TicTacToe game, every move the user needs to enter the board location where wants to place his/her marker. That board location is entered in a textfield.

I made all my gui and wrote my source code using netbeans gui builder.
Is this why i am not getting desired result?
Because, from the method i learnt in the stackOverflow post, it uses getContentPane().getComponents();.. So i tried to see what really fills in the component which is used hashed by Hashmap and the component actually has this value

javax.swing.JButton[,12,38,56x26,alignmentX=0.0,alignmentY=0.5,
border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1eec612,flags=296,maximumSize=,
minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,
margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],
paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,
rolloverSelectedIcon=,selectedIcon=,text=Gen,defaultCapable=true]

which is not what i need. I need actual names of textfields to be stored.And also, it does not access textfields too and just gives button details. When i ran getComponentCount() method in frame, i got 1 result(JButton) whereas there are actually 10 components.
In short, hashmap method is not working for me.

I'm going to illustrate two approaches using a small amount of code.
I'm just illustrating the concept, writing a robust implementation is up to you.

HashMap approach:

HashMap<String, /* insert type of your control */> board;
board.put("a1", /* reference to your control */);
board.put("a2", /* reference to your control */);
// etc.

You can write it shorter using loops, but you get the idea.

// get a control
control = board.get(pos.getText().toLowerCase());

Array approach:

Put your board controls in a 1D-array and compute the appropriate index from the user input.

String s = pos.getText();
// compute index in array
int loc = (Character.toLowerCase(s.charAt(0)) - 'a') * 3 + (s.charAt(1) - '1');

// get a control
control = board[loc];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.