I'm writing a program that lists a word taken randomly from an array of words and has a text field next to it. When the user types something in the text field and presses enter, it should perform an update that changes the word among some other tasks. However, when I run it, I can see that an action is registered as having been performed because I put in a println, but the JLable won't change its text.
Here's the code:
package com.jollex.TypingTest; import javax.swing.*; import java.util.Random; import java.awt.event.*; import java.io.*; public class Screen { JFrame frame; JPanel contentPane; static JLabel word; static JTextField ans; static int num = 270; static int[] numbers = new int[271]; static int wordsCorrect = 0; static int wordsTotal = 0; static boolean timerOn = false; boolean gameDone = false; static String[] words = new String[269]; public Screen() { //Create and set up the frame frame = new JFrame("Typing Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create a content pane contentPane = new JPanel(); //Create and add label word = new JLabel(newWord()); contentPane.add(word); //Create and add text field ans = new JTextField(20); contentPane.add(ans); //Creates action listener for text field and adds it ActionListener enter = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (timerOn == true) update(); //Test to see if action listener works (remove later) System.out.println("Action performed."); } }; ans.addActionListener(enter); //Add content pane to frame frame.setContentPane(contentPane); //Size and then display the frame frame.pack(); frame.setVisible(true); //Start timer timerOn = true; timer.start(); } //Creates a 60 second timer ActionListener updater = new ActionListener() { int count=0; public void actionPerformed(ActionEvent time) { if (count >= 60000) { timerOn = false; timer.stop(); gameDone = true; } count++; } }; Timer timer = new Timer(0, updater); /** *Updates the display: *Checks the answer *Sets the label to a new word *Selects all text in the text field. */ public static void update() { checkAns(); word.setText(newWord()); ans.selectAll(); } //Checks if the answer is correct and adds points accordingly. private static void checkAns() { String word = getCurrentWord(); String word2 = null; word2 = getAns(); if (word2.equals(word)) { wordsCorrect += 1; wordsTotal += 1; } else { wordsTotal += 1; } } //Returns a new word from the words array. public static String newWord() { return words[randomInt()]; } //Returns current word being displayed in the label. public static String getCurrentWord() { return words[num]; } //Returns text from text field. public static String getAns() { String userAns = null; userAns = ans.getText(); return userAns; } //Keeps an array of numbers from 0-269. Returns a random number and removes that number after it's been used. public static int randomInt() { for (int i = 0; i < 271; i++) { numbers[i] = i; } Random generator = new Random(); while (numbers[num] == 270) { num = generator.nextInt(270); } numbers[num] = 270; return num; } //Loads words from words.txt to a string array called words. private static void loadWords() { File wordFile = new File("words.txt"); FileReader in; BufferedReader readFile; String nextWord; try { in = new FileReader(wordFile); readFile = new BufferedReader(in); int i = 0; while ((nextWord = readFile.readLine()) != null && i < words.length) { words[i] = nextWord; i++; } readFile.close(); in.close(); } catch (FileNotFoundException e) { System.out.println("File does not exist or could not be found."); System.err.println("FileNotFoundException: " + e.getMessage()); } catch (IOException e) { System.out.println("Problem reading file."); System.err.println("IOException: " + e.getMessage()); } } //Creates and shows the GUI public static void runGUI() { JFrame.setDefaultLookAndFeelDecorated(true); @SuppressWarnings("unused") Screen screen = new Screen(); } public static void main(String[] args) { loadWords(); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { runGUI(); } }); } }