我用jbuilder7,建立了一个JTextArea的对象jta,在jta里头找不到addTextListener方法。我已经import java.awt.event.*;也已经implements TextListener和写了一个textValueChanged方法。但是jta.addTextListener(this);却不可以,我缺了点什么没有做吗?!

解决方案 »

  1.   

    JTextComponent(JTextArea/JTextField.....)用javax.swing.event.DocumentListener * The <code>java.awt.TextArea</code> could be monitored for changes by adding
     * a <code>TextListener</code> for <code>TextEvent</code>s. 
     * In the <code>JTextComponent</code> based
     * components, changes are broadcasted from the model via a
     * <code>DocumentEvent</code> to <code>DocumentListeners</code>. 
     * The <code>DocumentEvent</code> gives 
     * the location of the change and the kind of change if desired.
     * The code fragment might look something like:
     * <pre>
     *    DocumentListener myListener = ??;
     *    JTextArea myArea = ??;
     *    myArea.getDocument().addDocumentListener(myListener);
     * </pre>
     * <p>
     * For the keyboard keys used by this component in the standard Look and
     * Feel (L&F) renditions, see the
     * <a href="doc-files/Key-Index.html#JTextArea"><code>JTextArea</code> key assignments</a>.
      

  2.   

    我程序的目的是文本区每次得到更新,便用getText方法取它的文本,然后发送给每个客户端,用setText方法显示。我尝试过用DocumentListener,好像是行不通,文本编辑会出现问题,运行的时候也会出现异常所以我想试试TexitListener看看好不好使。
      

  3.   

    TextArea use TextListener
    JTextArea use DocumentListener
      

  4.   

    喔!!那么请问是不是在DocumentListener的insertUpdate等方法里头使用setText等方法不好使呀?!我运行的时候总是提示我setText不应该放在insertUpdate等方法里头。
    我现在已经改用按钮事件做发送了,不过我还是想知道有没有可能实时传输呢?!