Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Typing Tutor program

807600Nov 26 2007 — edited Nov 26 2007
Hi,

Trying to write a typing tutor program. As you type a character it checks if it matches with the JTextArea. If it does match, it takes a snapshot of the text in the JTextField. If its wrong it sets the text back to the snapshot.
However, when I set the text back to the snapshot it displays the key typed aswell. How do I remove this character, so that only the snapshot is visible.

Heres the code:
//file:TTutor.java 
//GUI typing tutor
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.String.*;



class TTutor extends JFrame{
    
    String[] phrase = {"In a world full of people", "Only some want to fly", "Isn't that crazy?"};

	final JTextArea area = new JTextArea();
	final JTextField field = new JTextField(40);
    String failString= new String("");
    int caretp;
    int x, y;
    GridBagConstraints constraints = new GridBagConstraints();
    int i = 0; 
   
    

    public TTutor(){
	super("TTutor v1.0");
	setSize(600, 300);
	setLocation(200, 200);
	
	setLayout(new GridBagLayout());
	area.setFont(new Font("Serif",Font.PLAIN, 18));
	area.setText(phrase[0]);
	area.setEditable(false);
	
	Container content = getContentPane();
	addGB(area, x=1, y=0);
	Image image = Toolkit.getDefaultToolkit().getImage("keyboard.jpg");
	addGB(new ImageComponent(image), x=1, y=1);
	addGB(field, x=1, y=2);
	setVisible(true);
	field.requestFocus();
	
	field.addKeyListener(new MyKeyListener());
	
      
	
       
	
    }

	void addGB(Component component, int x, int y){
	    constraints.gridx=x;
	    constraints.gridy=y;
	    add(component, constraints);
	}
	
    public void update(KeyEvent ke){
	char c = ke.getKeyChar();
	String cs = Character.toString(c);

	char ac = area.getText().charAt(i);
      String as = String.valueOf(ac);

      if (as.equals(cs)){
	  snapshot();
          i++;
	  System.out.println("match");
      }
      if (!as.equals(cs)){
	  wrong();
      System.out.println("wrong");
    }
    }
    void snapshot(){
       
	    failString = field.getText();
	caretp = field.getCaretPosition();
    }
    void wrong(){
	/*Probelm is here:
	  How do I get the textField to write the string minus the key typed
	*/
	 field.setText("");
	 field.setText(failString);
	 field.setCaretPosition(caretp);	
    }
    public static void main(String[] args){
	JFrame f = new TTutor();
	f.addWindowListener(new WindowAdapter(){
		public void windowClosing(WindowEvent we){System.exit(0);}
	    });
	f.setVisible(true);
	
    }
    
public class MyKeyListener extends KeyAdapter{
    public void keyTyped(KeyEvent ke){
  
	
	update(ke);
      	
     
      }		
      
    }
  }




















	
Regards ABourke.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 24 2007
Added on Nov 26 2007
1 comment
1,220 views