JTextArea已用DocumentListener更新 [英] JTextArea updated with DocumentListener

查看:140
本文介绍了JTextArea已用DocumentListener更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JTextArea area1 = new JTextArea();
JTextArea area2 = new JTextArea();
DocumentListener documentListener = new DocumentListener() {
      public void changedUpdate(DocumentEvent documentEvent) {
        printIt(documentEvent);
      }
      public void insertUpdate(DocumentEvent documentEvent) {
        printIt(documentEvent);
      }
      public void removeUpdate(DocumentEvent documentEvent) {
        printIt(documentEvent);
      }
      private void printIt(DocumentEvent documentEvent) {
        DocumentEvent.EventType type = documentEvent.getType();
        String typeString = null;
        if (type.equals(DocumentEvent.EventType.CHANGE)) {
          typeString = "(CHANGED KEY) ";
        }  else if (type.equals(DocumentEvent.EventType.INSERT)) {
          typeString = "(PRESSED KEY) ";
        }  else if (type.equals(DocumentEvent.EventType.REMOVE)) {
          typeString = "(DELETED KEY) ";
        }
        System.out.print("Type : " + typeString);
        Document source = documentEvent.getDocument();
        int length = source.getLength();
        System.out.println("Current size: " + length);

      }
    };
area1.getDocument().addDocumentListener(documentListener);
area2.getDocument().addDocumentListener(documentListener);

这是我在区域1或区域2中按东西时处理的代码.

This is my code for handling when things are pressed in either area1 or area2.

我正在尝试做到这一点,以便在更新一个区域的文本时,它以相同的文本更新第二个区域的文本,反之亦然.我将如何去做?一个字段用于加密内容,另一个字段用于解密值,反之亦然.

I am trying to make it so that when one area's text is updated, it updates the second area's text with the same text and vice versa. How would I go about doing so? One field is for encrypting something and the other for the decrypted values and vice versa.

推荐答案

只需让他们共享同一文档即可.

Just have them share the same Document, that's it.

area1.setDocument(area2.getDocument());

这篇关于JTextArea已用DocumentListener更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆