polaris
polaris

Reputation: 339

problems with swing timer

I'm creating an speed reader so I'm using a swingx.Timer object to display the words at a given time.I set the time to 1sec for testing. I had the code working before but then after closing eclipse and coming back to it the code doesn't work anymore. The timer does not call the actionlistener thus not performing the action it is supposed to perform. Here is my code. I read other questions talking about timer being a daemon thread and ending with main. However I don't think this is the case in my code but I could be wrong. Can someone help me point out my error?

package reader;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;   
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JMenu;
import java.awt.event.*;
import java.io.IOException;
import java.sql.Time;

import javax.swing.Timer;


public class ReaderPad extends JPanel{

public JButton startBtn, stopBtn, resetBtn;
public JList speedMenu;
public JMenuBar topMenu;
public JTextPane textPad;
public JScrollPane scroll;
public Timer timer;//to control the speed of the reader
public int speed; //the speed set for the reader, i.e. 100 words per minute
public JLabel speedLabel;
public JTextField speedText;
int wordLength; //size of text
String[] lines; //a line of text composed of 10 words from the text entered 
int wpm; 
int i;//index of word set being used

public ReaderPad(){
    this.setLayout(new BorderLayout());
    i = 0;

    speedLabel = new JLabel("words per minute");
    speedText = new JTextField();
    speedText.setColumns(3);
    startBtn  = new JButton("Start");
    stopBtn = new JButton("Stop");
    resetBtn = new JButton("Reset");
    textPad = new JTextPane();

    topMenu = new JMenuBar();


    textPad.setEditable(true);
    textPad.setFont(new Font("Serif", Font.PLAIN,25));
    textPad.setText("welcome to speed reader. Use CTRL + c to paste your text\n"
            + "Press start to begin");
    scroll = new JScrollPane(textPad);


    //create top menu bar
    topMenu.add(startBtn);
    topMenu.add(stopBtn);
    topMenu.add(resetBtn);
    topMenu.add(speedLabel);
    topMenu.add(speedText);

    //topMenu.add(speedMenu);

    this.add(topMenu, BorderLayout.NORTH);
    this.add(scroll, BorderLayout.CENTER);


    //set listeners for buttons  
    Actions listener = new Actions();
    startBtn.addActionListener(listener);
    stopBtn.addActionListener(listener);
    resetBtn.addActionListener(listener);

}//end of constructor


public void setFontSize(String size){


    Font font = textPad.getFont();
    String s = font.getName();
    textPad.setFont(new Font(s, Font.PLAIN, Integer.valueOf(size)));
}

public void setFontColor(String color){

}

//sets background color
public void setBackgroud(String color){

}

public void setSpeed(String speed){
    //speed = speed.replace("X", "");//remove the times symbol
    if(speed.isEmpty())//set default 
        this.speed = 100;
    else
        this.speed = Integer.valueOf(speed);
}

public int getSpeed(){

    return this.speed;
}


public void startReader(){//words per minute


    int speed = getSpeed();//wpm selected by the user
    System.out.println("speed: " + speed);
    String text = textPad.getText();
//  System.out.println(text);

    lines = text.split(" ");
    wordLength = lines.length;//get the number of words in the text
    System.out.println("length: "+ wordLength);
    //System.out.println(wordLength.length);
    if(text.isEmpty()){
        textPad.setText("You didn't enter any text in the text area.\n"
                +"Please enter some text to begin.");
    }
    else{//set timer
        //calculate speed first: time = (speed chosen * number of workds in text) = (sec/words)*wordsize
        //wpm = (1/speed)*wordLength*60*1000; //multiply by 60 secons
        wpm = 1000;
        System.out.println("wpm: "+ wpm);
        TimerReader counter = new TimerReader();
        timer = new Timer(1000, counter);
        timer.start();
        System.out.println(timer.isRunning());
        }
    }

public void stopReader(){

}

//listener class
public class Actions implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        String source = e.getActionCommand();//read the button asking for the request

        if(source.equals("Reset")){
            textPad.setText("");
            if(timer.isRunning())
                timer.stop();
        }
        else if(source.equals("Start")){
            //first read the speed selected and set the speed 
            setSpeed(speedText.getText());
            startReader();
        }

        else if (source.equals("Stop"))
            i = 0;
            timer.stop();
    }
}

public class TimerReader implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e){
        //once the timer is called the textpad starts displaying the words

        String s = lines[i];    
        i++;
        textPad.setText(s);
        System.out.println(i);
        System.out.println(e.getActionCommand());
        System.out.print(textPad.getText());

    }
}//end of inner class

}//end of ReaderPad class

Upvotes: 0

Views: 54

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

Remove timer.stop() from the end of Actions actionPerformed method...

public class Actions implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        String source = e.getActionCommand();//read the button asking for the request

        if (source.equals("Reset")) {
            textPad.setText("");
            if (timer.isRunning()) {
                timer.stop();
            }
        } else if (source.equals("Start")) {
            //first read the speed selected and set the speed 
            setSpeed(speedText.getText());
            startReader();
        } else if (source.equals("Stop")) {
            i = 0;
        }
        // This is stopping the timer AFTER it was already started
        //timer.stop();
    }
}

Upvotes: 4

Related Questions