2

我正在使用JTextPane学习Java 中的DocumentListener。我正在尝试获取最近在文本窗格中插入/删除的当前字符串或 单词(文本) 。我试过这个:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;

class ChangeEffect implements DocumentListener {
    @Override
    public void insertUpdate(DocumentEvent de) {
        int offset = de.getOffset();
        try {
              String str = de.getDocument().getText(offset,de.getLength());
              System.out.println("You entered the text: " + str);
        }catch(Exception e){e.printStackTrace();}
    }
    @Override
    public void changedUpdate(DocumentEvent de) {
    }
    @Override
    public void removeUpdate(DocumentEvent de) {
        try {
            String s  = de.getDocument().getText(de.getOffset(),de.getLength());
           System.out.println("Removed text is: " +s);
        }catch(Exception e){}
    }
}


public class MultiModeColor {
    private static void displayGUI() {
        JFrame frame = new JFrame("Demo");
        JTextPane text = new JTextPane();
        frame.add(text);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
        text.getDocument().addDocumentListener(new ChangeEffect());
    }

    public static void main(String[] arg) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                displayGUI();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

在上面的例子中,我得到了最近插入的文本,但删除的文本什么都不显示(除了 println 语句中的消息)。
有没有办法从文档中获取最近删除的文本?

4

2 回答 2

3

尝试使用 aDocumentFilter而不是DocumentListener. 您面临的主要问题是DocumentListener事后收到通知,这意味着当您收到通知时,文本已经被删除。

另一方首先DocumentFilter得到通知......

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class DocumentMonitor {

    public static void main(String[] args) {
        new DocumentMonitor();
    }

    public DocumentMonitor() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTextArea ta = new JTextArea(20, 20);
                ((AbstractDocument) ta.getDocument()).setDocumentFilter(new DocumentFilter() {

                    @Override
                    public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                        if (offset >= fb.getDocument().getLength()) {
                            System.out.println("Added: " + text);
                        } else {
                            String old = fb.getDocument().getText(offset, length);
                            System.out.println("Replaced " + old + " with " + text);
                        }
                        super.replace(fb, offset, length, text, attrs);
                    }

                    @Override
                    public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
                        System.out.println("Added: " + text);
                        super.insertString(fb, offset, text, attr);
                    }

                    @Override
                    public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException {
                        System.out.println("Removed: " + fb.getDocument().getText(offset, length));
                        super.remove(fb, offset, length);
                    }

                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(ta));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

是的,这是使用 a JTextArea,但过程是相同的 a JTextPane...

于 2013-10-07T05:43:55.477 回答
-1

尝试

package com.linkage.test;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

class ChangeEffect implements DocumentListener {
    private String text;
    @Override
    public void insertUpdate(DocumentEvent de) {
        int offset = de.getOffset();
        try {
              String str = de.getDocument().getText(offset,de.getLength());
              text = de.getDocument().getText(0,offset + 1);
              System.out.println("You entered the text: " + str);
        }catch(Exception e){e.printStackTrace();}
    }
    @Override
    public void changedUpdate(DocumentEvent de) {
    }
    @Override
    public void removeUpdate(DocumentEvent event) {
         try {
             String str = text.substring(event.getOffset(),event.getOffset() + event.getLength());
             System.out.println("Removed text is: " + str);
         } catch (Exception ex) {
             ex.printStackTrace();
         }
    }

}
于 2013-10-07T05:29:18.920 回答