1 package sample.google.spellcheck; 2 3 import java.awt.Color ; 4 import java.awt.GridBagConstraints ; 5 import java.awt.GridBagLayout ; 6 import java.awt.event.KeyEvent ; 7 import java.awt.event.KeyListener ; 8 9 import javax.swing.JScrollPane ; 10 import javax.swing.JTextArea ; 11 import javax.swing.JTextField ; 12 13 19 public class AsyncPanel extends javax.swing.JPanel implements Observer,KeyListener { 20 FormModel formModel; 21 JTextArea writingTextArea; 22 JTextArea displayTextArea; 23 JTextField errorMessageField; 24 public AsyncPanel() 25 { 26 GridBagLayout gbLayout = new GridBagLayout (); 27 GridBagConstraints constraint = new GridBagConstraints (); 28 this.setLayout(gbLayout); 29 30 formModel = new FormModel(this); 31 32 writingTextArea = new JTextArea (); 33 writingTextArea.setLineWrap(true); 34 35 displayTextArea = new JTextArea (); 36 displayTextArea.setEditable(false); 37 displayTextArea.setLineWrap(true); 38 39 errorMessageField = new JTextField (); 40 errorMessageField.setEditable(false); 41 errorMessageField.setBackground(Color.LIGHT_GRAY); 42 errorMessageField.setForeground(Color.RED); 43 44 JScrollPane scrollPaneGet = new JScrollPane (writingTextArea); 45 JScrollPane scrollPaneSet = new JScrollPane (displayTextArea); 46 47 writingTextArea.setText("Enter a String"); 48 writingTextArea.addKeyListener(this); 49 50 constraint.fill = GridBagConstraints.BOTH; 51 constraint.gridx=0; 52 constraint.weightx=1; 53 constraint.weighty=8; 54 gbLayout.setConstraints(scrollPaneGet,constraint); 55 this.add(scrollPaneGet); 56 gbLayout.setConstraints(scrollPaneSet,constraint); 57 this.add(scrollPaneSet); 58 constraint.weighty=1; 59 gbLayout.setConstraints(errorMessageField,constraint); 60 this.add(errorMessageField); 61 62 63 } 64 65 public void update(String message) 66 { 67 displayTextArea.setText(displayTextArea.getText() + " " + message); 68 } 69 70 public void updateError(String message) { 72 errorMessageField.setText(message); 73 } 74 75 76 public void keyPressed(KeyEvent e) { 77 int key=e.getKeyChar(); 78 if((key==KeyEvent.VK_SPACE)||(key==KeyEvent.VK_ENTER)){ 79 String [] words=writingTextArea.getText().split("\\s"); 80 if (words.length > 0) 81 formModel.doAsyncSpellingSuggestion(words[words.length-1]); 82 } 83 } 84 85 public void keyReleased(KeyEvent e) { 86 } 87 88 public void keyTyped(KeyEvent e) { 89 } 90 91 public void clear() { 92 displayTextArea.setText(""); 93 writingTextArea.setText(""); 94 errorMessageField.setText(""); 95 } 96 } 97 | Popular Tags |