我正在使用 JTextPane 了解java中的 DocumentListener 。我正在尝试获取当前字符串或
最近在textpane中插入/删除的单词(文本)。我试过这个:
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语句中的消息除外) 有没有办法从文档中获取最近删除的文本?
答案 0 :(得分:2)
尝试使用DocumentFilter
代替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);
}
});
}
}
是的,这是使用JTextArea
,但JTextPane
的流程相同......
答案 1 :(得分:0)
试
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();
}
}
}