TravJav92
TravJav92

Reputation: 117

Read JTextArea For Jazzy Spell Checker API

Question: Trying to get the same effect as the code below only with JTextArea so I want the JTextArea to be read and spelling suggestions to be recommended every time the user types a new misspelt word.

Below is the working example with 'System.in' which works well.

(Vars userField = JTextArea & dic.txt is a list of the english language for the system to use for suggestions)

CODE (1)

public SpellCheckExample() {
    try {
      SpellDictionary dictionary = new SpellDictionaryHashMap(new File(dic.txt));

      spellCheck = new SpellChecker(dictionary);
      spellCheck.addSpellCheckListener(this);
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

      while (true) {
        System.out.print("Enter text to spell check: ");
        String line = in.readLine();

        if (line.length() <= 0)
          break;
        spellCheck.checkSpelling(new StringWordTokenizer(line));
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

What I have Been trying:

CODE (2)

public void spellChecker() throws IOException{


      String userName = System.getProperty("user.home");
      SpellDictionary dictionary = new SpellDictionaryHashMap(new File(userName+"/NetBeansProjects/"+"/project/src/dic.txt"));
      SpellChecker spellCheck = new SpellChecker(dictionary);
      spellCheck.addSpellCheckListener(this);


    try{
    StringReader sr = new StringReader(userField.getText());
    BufferedReader br = new BufferedReader(sr);

    while(true){
   String line = br.readLine();

   if(line.length()<=0)
   break;
  spellCheck.checkSpelling(new StringWordTokenizer(line));


            }
   }catch(IOException e){
       e.printStackTrace();
   }

 }

March 3rd 2016 (Update)

    public void spellChecker() throws IOException{
  // getting context from my dic.txt file for the suggestions etc.
    SpellDictionary dictionary = new SpellDictionaryHashMap(new File("/Users/myname/NetBeansProjects/LifeSaver/src/dic.txt"));
    SpellChecker spellCheck = new SpellChecker(dictionary);


    // jt = JTextField already defined in constructors and attemtpting to pass this into system and 
   InputStream is = new ByteArrayInputStream(jt.getText().getBytes(Charset.forName("UTF-8"))); 


  //spellCheck.checkSpelling(new StringWordTokenizer(line));  ""ORIGINAL"""



  // reccomending cast to wordfinder
spellCheck.checkSpelling(new StringWordTokenizer(is);
      }   

Upvotes: 0

Views: 413

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347214

Take a look at Concurrency in Swing for reasons why your current approach won't work, then have a look at Listening for Changes on a Document and Implementing a Document Filter for some possible solutions

As someone is bound to mention it, DON'T use a KeyListener, it's not an appropriate solution for the problem

Put simpler, Swing is a single threaded, event driven framework. So anything you do which blocks the Event Dispatching Thread, will prevent it from processing new events, including paint events, making your UI unresponsive

As an event driven environment, you need to register interested in been notified when some event occurs (this is an example of Observer Pattern) and then take appropriate actions based on those events.

Remember though, you can not make changes to a Document via a DocumentListener, so be careful there

Upvotes: 3

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You don't want to try to drop console UI code into an event-driven GUI, as it will never work like that. Instead you need to use GUI events to trigger your actions, not readln's.

The first thing you must decide on is which event you wish to use to trigger your spell check. For my money, I'd get the user's input in a JTextField, not a JTextArea since with the former, we can easily trap <enter> key presses by adding an ActionListener on the JTextField. You can always use both, and then once the text is spell checked, move it to the JTextArea, but this is exactly what I'd recommend:

  • use a JTextField,
  • add an ActionListener to the JTextField to be notified whenever the field has focus and enter is pressed,
  • within this listener, extract the text from the JTextField, by calling getText() on the field
  • Then run your spell check code on extracted text,
  • and output the result into a nearby JTextArea.

Upvotes: 3

Related Questions