简体   繁体   English

阅读JTextArea for Jazzy Spell Checker API

[英]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. 问题:尝试仅使用JTextArea来获得与下面的代码相同的效果,因此我希望每次用户键入新的拼写错误的单词时都阅读JTextArea并提出拼写建议。

Below is the working example with 'System.in' which works well. 下面是带有“ System.in”的工作示例,该示例运行良好。

(Vars userField = JTextArea & dic.txt is a list of the english language for the system to use for suggestions) (Vars userField = JTextArea&dic.txt是系统用于建议的英语列表)

CODE (1) 代码(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) 代码(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) 2016年3月3日(更新)

    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);
      }   

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 看看Swing中的并发性为何会导致您当前的方法行不通,然后看看“ 侦听文档中的更改实施文档过滤器”以获取一些可能的解决方案

As someone is bound to mention it, DON'T use a KeyListener , it's not an appropriate solution for the problem 就像一定要提到的那样,不要使用KeyListener ,这不是解决该问题的合适方法

Put simpler, Swing is a single threaded, event driven framework. 简而言之,Swing是一个单线程,事件驱动的框架。 So anything you do which blocks the Event Dispatching Thread, will prevent it from processing new events, including paint events, making your UI unresponsive 因此,您执行的任何阻止事件调度线程的操作都将阻止其处理新事件(包括绘画事件),从而使UI无响应

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. 作为事件驱动的环境,您需要在某个事件发生时注册感兴趣的通知(这是Observer Pattern的示例),然后根据这些事件采取适当的措施。

Remember though, you can not make changes to a Document via a DocumentListener , so be careful there 不过请记住,您不能通过Document来对DocumentListener进行更改,因此请当心

You don't want to try to drop console UI code into an event-driven GUI, as it will never work like that. 您不想尝试将控制台UI代码放入事件驱动的GUI中,因为它永远不会那样工作。 Instead you need to use GUI events to trigger your actions, not readln's. 相反,您需要使用GUI事件来触发您的动作,而不是readln。

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. 为了我的钱,我将用户输入输入到JTextField中,而不是JTextArea中,因为使用前者,我们可以通过在JTextField上添加ActionListener轻松捕获<enter>按键。 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: 您可以始终使用两者,然后在对文本进行拼写检查后,将其移至JTextArea,但这正是我所建议的:

  • use a JTextField, 使用一个JTextField
  • add an ActionListener to the JTextField to be notified whenever the field has focus and enter is pressed, 将一个ActionListener添加到JTextField中,以在该字段具有焦点并按下Enter时得到通知,
  • within this listener, extract the text from the JTextField, by calling getText() on the field 在此侦听器中,通过在字段上调用getText()从JTextField中提取文本
  • Then run your spell check code on extracted text, 然后对提取的文本运行您的拼写检查代码,
  • and output the result into a nearby JTextArea. 并将结果输出到附近的JTextArea中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM