I have an array of TextFields added to a table by a loop. The number of TextFields added depends on an input given by the user.
The TextFields are initialised with the text "Player #" (# is incremented by the loop).
I want to be able to remove this text from each text field when they are focused, and if nothing is typed upon losing focus it will revert to the original string.
My problem here is being able to access the TextField to set the text in the FocusListener.
Here is my code: (NOTE: playernames is a table defined outside the code snippet.)
for(int i=0; i<MainMenu.numplayers; i++) {
playername[i] = new TextField("Player " + (i+1), skin);
playernames.add(new Label("Player " + (i+1) + ":", skin, "black")).padRight(5.0F).padBottom(5.0F);
playernames.add(playername[i]).width(125.0F).padBottom(5.0F);
playernames.row();
playername[i].addListener(new FocusListener() {
public void keyboardFocusChanged(FocusListener.FocusEvent event, Actor actor, boolean focused) {
if(focused == true) {
/** change text to "" */
}
else if(focused==false){
/** change text back to "Player #" if nothing was typed */
}
}
});
How can I access the TextField inside the Listener to be able to change the text?