使用DocumentFilter.FilterBypass [英] Using DocumentFilter.FilterBypass

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

问题描述

我希望在我的 DocumentFilter

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

目前为了获得FilterBypass的实例(上面方法中的byPass),我需要从重写方法insertString获取:

Currently in order to get an instance of FilterBypass (byPass on method above) , I need to get from the overridden method 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的不同方法吗?我找不到以不同方式获取对 FilterBypass 的引用的方法。

But this is causing me some problems. Can anyone suggest some different way of getting a FilterBypass? I can't find a way to get a reference to the FilterBypass differently.

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

If I was to override its methods how should it be?

推荐答案

例如,这是一个带有DocumentFilter的SSCCE,可防止用户在文档中输入数字但允许Swing Timer执行此操作。

For instance, here's an SSCCE with a DocumentFilter which prevents the user from typing numbers into the document but allows the Swing Timer to do so.

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

   }
}

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

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