2

我想在我的身上有这样的方法DocumentFilter

public void replaceUpdate(int offset, int length, String text) {
        try {
            super.replace(byPass, offset, length, text, null);
        } catch (BadLocationException ex) {
            //error
        }
}

目前,为了获取 FilterBypass 的实例(通过上述方法通过),我需要从被覆盖的方法 insertString 中获取:

private FilterBypass byPass;

@Override
    public void insertString(DocumentFilter.FilterBypass fb,
            int offset, String string, AttributeSet att)
            throws BadLocationException {
        byPass = fb;
        //some stuff here
        super.insertString(fb, offset, string, att);
    }

但这给我带来了一些问题。任何人都可以提出一些不同的方法来获得过滤器旁路吗?我找不到一种方法来获得FilterBypass不同的参考。

如果我要覆盖它的方法应该怎么做?

4

2 回答 2

10

例如,这里有一个带有 DocumentFilter 的 SSCCE,它可以防止用户在文档中输入数字,但允许 Swing Timer 这样做。

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

public class DocFilterPanel extends JPanel {
   private JTextArea textArea = new JTextArea(12, 50);
   private MyDocFilter myDocFilter = new MyDocFilter();

   public DocFilterPanel() {
      ((PlainDocument) textArea.getDocument()).setDocumentFilter(myDocFilter);
      int vsbPolicy = JScrollPane.VERTICAL_SCROLLBAR_ALWAYS;
      int hsbPolicy = JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;
      add(new JScrollPane(textArea, vsbPolicy , hsbPolicy));

      int timerDelay = 1000;
      new Timer(timerDelay , new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            myDocFilter.setFilter(false);
            textArea.append("12345\n");
            myDocFilter.setFilter(true);
         }
      }).start();
   }

   private static void createAndShowGui() {
      DocFilterPanel docFilterPanel = new DocFilterPanel();

      JFrame frame = new JFrame("DocFilterTest");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(docFilterPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class MyDocFilter extends DocumentFilter {
   private static final String REMOVE_REGEX = "\\d";
   private boolean filter = true;

   public boolean isFilter() {
      return filter;
   }

   public void setFilter(boolean filter) {
      this.filter = filter;
   }

   @Override
   public void insertString(FilterBypass fb, int offset, String text,
         AttributeSet attr) throws BadLocationException {
      if (filter) {
         text = text.replaceAll(REMOVE_REGEX, "");
      }
      super.insertString(fb, offset, text, attr);

   }

   @Override
   public void replace(FilterBypass fb, int offset, int length, String text,
         AttributeSet attrs) throws BadLocationException {
      if (filter) {
         text = text.replaceAll(REMOVE_REGEX, "");
      }
      super.replace(fb, offset, length, text, attrs);

   }
}
于 2012-02-19T03:22:57.597 回答
0

我喜欢@hovercraft 的解决方案,但它给了我并发问题。我通过在文本区域上同步过滤器切换来解决这个问题。像这样:

synchronized (textArea) {
    myDocFilter.setFilter(false);
    textArea.append("12345\n");
    myDocFilter.setFilter(true);
}
于 2014-03-06T14:25:19.967 回答