2013-10-07 73 views
2

我正在學習有關DocumentListener in java與JTextPane。我試圖獲取當前字符串或
字(文本),它是最近在textpane中插入/刪除。我已經試過這樣:在JTextPane中查找刪除的文本

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

你可以嘗試使用'DocumentFilter'改爲... – MadProgrammer

回答

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一樣...

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

} 
+0

此解決方案不適用於我,在removeUpdate調用之前更新文本內容,因此打印的字符串不正確。我正在使用Java 1.8和Jython 2.7.0,所以我沒有測試過純Java實現。 – sgrieve