0

Here is the image of my application:

image

When the new button is pressed the question will be displayed and timer starts.

But after the 'New' button is pressed neither timer runs nor text is displayed and the default close operation does not work. When i comment out the checkTimer() then it is working fine.

Here is the code:

if(buttonEvent.getActionCommand().equals("New")){
                String store = buttonEvent.getActionCommand();
                startGame();
                checkTimer();

            panelOne.remove(buttonNew);
            panelOne.revalidate();
            panelOne.repaint();
        }

public void startGame(){
    // TODO Auto-generated method stub
    String line = null;
    try{
        BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\myPc\\Documents\\myFile.txt"));
        while((line = reader.readLine()) != null){
            System.out.println(line);
            queue.add(line);
        }
        reader.close();
    }
    catch(IOException exception){
        exception.printStackTrace();
    }
       // flag = true;
        String display = queue.remove();
        textArea.setText(display);
        //checkTimer();
} 


    public void checkTimer() {
    // TODO Auto-generated method stub

    int sec = 59;
    int min = Integer.parseInt(timerField1.getText());

    while(min >= 0){
        min--;
        if(min >= 0){
            for(int i = 0; i < 60; i++){
                try{
                    Thread.sleep(1000);
                }
                catch(InterruptedException ie){
                    Thread.currentThread().interrupt();
                }

                timerField1.setText(Integer.toString(min));
                timerField2.setText(Integer.toString(sec));
                if(sec > 0){
                    sec--;
                }
                else{
                    break;
                }
            }
        }
        //textArea1.setText(Integer.toString(num));
        sec = 59;
    }

}

Please guide me that why its not working.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
user007
  • 88
  • 1
  • 9
  • Check the [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) tutorial – Robin Jul 28 '17 at 13:52
  • What is the app supposed to do? Why are you using threads? Have you read how to use threads? Which threads are responsible for which parts of your application? – Carl G Jul 28 '17 at 14:39

3 Answers3

3

You block EventDispatcherThread in your checkTimer() by calling sleep(). In fact it prevents repainting UI and processing all events.

Instead start a new separate thread and call the UI changes e.g.

            timerField1.setText(Integer.toString(min));
            timerField2.setText(Integer.toString(sec));

inside SwingUtilities.invokeAndWait() block (could also try invokeLater())

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • hey thanks for replying i tried creating a separate thread but eclipse gave error message to create a static thread and i am unable to understand the java swing timers so that is the problem :( – user007 Jul 28 '17 at 18:20
2
Thread.sleep(1000);

Don't use Thead.sleep when code is executing on the Event Dispatch Thread (EDT). This will cause the GUI to freeze and prevent the GUI from responding to events.

When the new button is pressed the question will be displayed and timer starts.

Then you should be using a Swing Timer. You would set the Timer to fire every second so you can update the time. Then after if fires 300 times you would stop the Timer and the game would be over.

Read the section from the Swing tutorial on How to Use Swing Timers for more information and working examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • i read that section but i couldn't get the clear idea on how to use them. – user007 Jul 28 '17 at 18:23
  • @user007, simple example to get you started: https://stackoverflow.com/questions/30417719/update-a-label-with-a-swing-timer/30417857#30417857. You can also search the forum for other examples. I can't guess what you don't understand. – camickr Jul 28 '17 at 19:03
-1

Your problem is that you checkTimer() it's not a thread, then if you press New button this call again checkTimer() but, the other instance is running yet. You need Use Swing Timer for your clock and then when you press New button you can stop it.

  • 1
    (1-) Don't extend Thread to create your own "Timer". All Swing components should be updated from the EDT (as is stated in the Concurrency tutorial). Instead, you should be using a Swing Timer. The Swing tutorial also has a section on "How to Use Swing Timers". – camickr Jul 28 '17 at 14:01
  • You right!!, I forgot this. Using Swing timer is easiest than thread. – Fabian Rodriguez Jul 28 '17 at 14:12