Search code examples
javastringswingiojtextfield

JTextField getText() not working


I've been looking all over, and i cant find anyone who can solve this problem. I'm making a game, and in that game, i have editable controls. the controls window is a seperate JFrame, and when i click the confirm button, it is supposed to write the items in the JTextFields (holding the controls) to a file. but that wasnt working, so instead i have it print the arraylist that holds the values. here is the code:

public void writeControls() {
    ArrayList<String> al = new ArrayList<String>();

    al.add(up.getText());
    al.add(down.getText());
    al.add(left.getText());
    al.add(right.getText());
    al.add(jump.getText());
    al.add(duck.getText());
    al.add(attack.getText());

    for (int i = 0; i < al.size(); i++) {
         System.out.println(al.get(i));
    }
    System.exit(0);
}

the problem is this: if i change the final JTextField attack or any other one for that matter, and click submit, the system prints out the default controls. for example, if the JTextFields have the values w,a,s,d,r,t,q and i change the value q to i, it prints out q. what am i doing wrong? thanks in advance!

EDIT 1:

code for the textfields, and the FILES.... is simply a string stored in a different class. the class setText() is below the textfields.

up = new JTextField(setText(FILES.controlsFileFinalDir, 1));
    down = new JTextField(setText(FILES.controlsFileFinalDir, 2));
    left = new JTextField(setText(FILES.controlsFileFinalDir, 3));
    right = new JTextField(setText(FILES.controlsFileFinalDir, 4));
    jump = new JTextField(setText(FILES.controlsFileFinalDir, 5));
    duck = new JTextField(setText(FILES.controlsFileFinalDir, 6));
    attack = new JTextField(setText(FILES.controlsFileFinalDir, 7));

public String setText(String fileDir, int lineNum) {
    String txt = "";
    txt = io.readSpecificLine(fileDir, lineNum);
    txt = switchCase(txt);

    return txt;
}

switchcase() is only taking what you have written in the text file that these are getting the values from, and translating them. so if the value is 0, it is turned into Space, etc. io.readSpecificLine(); is only to get the line of text from the file. does this help?

EDIT 2: i just was dinking around and found out that if i set the JTextField text by using setText(""); then use getText(); it works. so the problem is that when i change it manually, and use getText(); it wont work. Why?


Solution

  • SOLUTION none of the above answers were working for me, so i finally decided to just start over with that class. the few things i changed were the way i made the JTextFields. I made them as an array instead of individual objects. Second is the way i put what they say. When i initialized them, i was unable to get them to create WITH the text in the parameters. so i had to do that seperately. i changed some of the method names so as to reduce future confusion, and it worked! so im not sure what was up with that, maybe it was the way i did it, maybe just a fluke. it happens sometimes, so im sorry for the delay and waste of your time! thanks for all the answers anyway!