DocumentFilter的每个方法(和参数)到底做了什么?

时间:2015-12-18 01:46:16

标签: java swing documentfilter

我一直在努力围绕这个DocumentFilter业务,当我觉得我大部分时都理解它时,我尝试了一个简单的测试用例,但没有任何意义。

因此,最初的目标是创建一个简单的DocumentFilter以仅允许数字,包括小数。我做了一些研究,并阅读了StackOverflow上已有的几篇帖子,详细介绍了这个过程。感谢那些发帖的人。我也第一次阅读了Regex,并且对于它的多样性感到震惊。

继续,我写了(大部分是复制的)其中一个例子并在我的程序中试用了它,但由于某种原因它只是不起作用。我决定回到基础并尝试一个非常简单的DocumentFilter子类:

public class SomeFilter extends DocumentFilter {

    public void insertString(FilterBypass fb, int offs, String str,
        AttributeSet a) throws BadLocationException {

        if (str.matches("B")) super.insertString(fb, offs, str, a);
    }

    public void replace(FilterBypass fb, int offs, int len, String str,
        AttributeSet a) throws BadLocationException {

        if (str.matches("A")) super.replace(fb, offs, len, str, a);
    }
}

这真是让我失望的原因。从oracle文件中,我读到insertString是插入东西(我假设光标在哪里)并且替换是替换(例如用于复制和粘贴)。现在显然我在这里误解了一些东西,因为我使用setDocumentFilter的JTextField只让我“插入”A而不是B'。有人可以解释一下这些方法是如何运作的吗?

如果我能解决这个问题,我会稍后发布我对实际IntegerFilter所做的事情,看看我是否能够解决这个问题。

2 个答案:

答案 0 :(得分:2)

当您在JTextComponent中输入文字时,replace(...)的{​​{1}}方法将被调用。这是因为DocumentFilter的{​​{1}}方法最终会被文本插入操作调用。

如果您使用replaceSelection(...)的{​​{1}}方法将文字直接插入JTextComponent,则会调用Document的{​​{1}}方法。

答案 1 :(得分:1)

可以找到<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen" xmlns:local="clr-namespace:WpfApplication1"> <Grid> <Canvas > <Canvas.Background> <VisualBrush TileMode="Tile" Stretch="Uniform" Viewport="20,20,20,20" ViewportUnits="Absolute"> <VisualBrush.Visual> <Rectangle Width="20" Height="20" Fill="sc#1,0.01,0.01,.01" Stroke="sc#1,0.02,0.02,.02" StrokeThickness="0.1"/> </VisualBrush.Visual> </VisualBrush> </Canvas.Background> <local:ShapeNode Canvas.Left="117" Canvas.Top="84"/> <local:ShapeNode Canvas.Left="242" Canvas.Top="183"/> </Canvas> </Grid> </Window> 课程的文档here课程中有三种方法:

InsertString():

<UserControl x:Class="WpfApplication1.ShapeNode"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Ellipse Fill="Red" Opacity=".2" Height="150" Width="150"></Ellipse>
        <Border Margin="5" Height="50" Width="100" Background="#FFDE6119" CornerRadius="5"></Border>
        <TextBlock VerticalAlignment="Center" Background="Transparent" Text="Donuts" HorizontalAlignment="Center"></TextBlock>
    </Grid>
</UserControl>
  

在将文本插入指定文档之前调用。

删除():

2015-12-15T21:30:18.546+0800 b.s.d.executor [INFO] Activating spout emitSpout:(1)
2015-12-15T21:30:18.578+0800 b.s.d.executor [INFO] Preparing bolt __system:(-1)
2015-12-15T21:30:18.614+0800 b.s.d.executor [INFO] Prepared bolt __system:(-1)
2015-12-15T21:38:37.453+0800 b.s.m.n.Client [ERROR] failed to send 943 messages to Netty-Client-eda.local/192.168.137.2:6708: java.lang.OutOfMemoryError: Direct buffer memory
2015-12-15T21:38:37.457+0800 b.s.m.n.Client [ERROR] failed to send 944 messages to Netty-Client-eda.local/192.168.137.2:6708: java.nio.channels.ClosedChannelException
2015-12-15T21:38:37.464+0800 b.s.m.n.Client [ERROR] failed to send 945 messages to Netty-Client-eda.local/192.168.137.2:6708: java.nio.channels.ClosedChannelException
2015-12-15T21:38:37.472+0800 b.s.m.n.Client [ERROR] failed to send 944 messages to Netty-Client-eda.local/192.168.137.2:6708: java.nio.channels.ClosedChannelException
2015-12-15T21:38:37.477+0800 b.s.m.n.Client [ERROR] failed to send 943 messages to Netty-Client-eda.local/192.168.137.2:6708: java.nio.channels.ClosedChannelException
2015-12-15T21:38:37.479+0800 b.s.m.n.Client [ERROR] failed to send 938 messages to Netty-Client-eda.local/192.168.137.2:6708: java.nio.channels.ClosedChannelException
2015-12-15T21:38:37.481+0800 b.s.m.n.Client [ERROR] failed to send 946 messages to Netty-Client-eda.local/192.168.137.2:6708: java.nio.channels.ClosedChannelException
2015-12-15T21:38:37.482+0800 b.s.m.n.Client [ERROR] failed to send 943 messages to Netty-Client-eda.local/192.168.137.2:6708: java.nio.channels.ClosedChannelException
2015-12-15T21:38:37.484+0800 b.s.m.n.Client [ERROR] failed to send 943 messages to Netty-Client-eda.local/192.168.137.2:6708: java.nio.channels.ClosedChannelException
2015-12-15T21:38:37.485+0800 b.s.m.n.Client [ERROR] failed to send 941 messages to Netty-Client-eda.local/192.168.137.2:6708: java.nio.channels.ClosedChannelException
2015-12-15T21:38:37.490+0800 b.s.m.n.Client [ERROR] failed to send 942 messages to Netty-Client-eda.local/192.168.137.2:6708: java.nio.channels.ClosedChannelException
2015-12-15T21:38:37.504+0800 b.s.m.n.Client [ERROR] failed to send 941 messages to Netty-Client-eda.local/192.168.137.2:6708: java.nio.channels.ClosedChannelException
2015-12-15T21:38:37.505+0800 b.s.m.n.Client [ERROR] failed to send 943 messages to Netty-Client-eda.local/192.168.137.2:6708: java.nio.channels.ClosedChannelException
2015-12-15T21:38:37.507+0800 b.s.m.n.Client [ERROR] failed to send 944 messages to Netty-Client-eda.local/192.168.137.2:6708: java.nio.channels.ClosedChannelException
2015-12-15T21:38:37.510+0800 b.s.m.n.Client [ERROR] failed to send 944 messages to Netty-Client-eda.local/192.168.137.2:6708: java.nio.channels.ClosedChannelException
2015-12-15T21:38:37.516+0800 b.s.m.n.StormClientHandler [INFO] Connection failed Netty-Client-eda.local/192.168.137.2:6708
java.lang.OutOfMemoryError: Direct buffer memory
    at java.nio.Bits.reserveMemory(Bits.java:658) ~[na:1.7.0_67]
    at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123) ~[na:1.7.0_67]
    at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:306) ~[na:1.7.0_67]
    at org.apache.storm.netty.buffer.ChannelBuffers.directBuffer(ChannelBuffers.java:167) ~[storm-core-0.9.6.jar:0.9.6]
    at org.apache.storm.netty.buffer.ChannelBuffers.directBuffer(ChannelBuffers.java:151) ~[storm-core-0.9.6.jar:0.9.6]
    at backtype.storm.messaging.netty.MessageBatch.buffer(MessageBatch.java:84) ~[storm-core-0.9.6.jar:0.9.6]
    at backtype.storm.messaging.netty.MessageEncoder.encode(MessageEncoder.java:32) ~[storm-core-0.9.6.jar:0.9.6]
    at org.apache.storm.netty.handler.codec.oneone.OneToOneEncoder.doEncode(OneToOneEncoder.java:66) ~[storm-core-0.9.6.jar:0.9.6]
    at org.apache.storm.netty.handler.codec.oneone.OneToOneEncoder.handleDownstream(OneToOneEncoder.java:59) ~[storm-core-0.9.6.jar:0.9.6]
    at org.apache.storm.netty.channel.DefaultChannelPipeline.sendDownstream(DefaultChannelPipeline.java:591) ~[storm-core-0.9.6.jar:0.9.6]
    at org.apache.storm.netty.channel.DefaultChannelPipeline.sendDownstream(DefaultChannelPipeline.java:582) ~[storm-core-0.9.6.jar:0.9.6]
    at org.apache.storm.netty.channel.Channels.write(Channels.java:704) ~[storm-core-0.9.6.jar:0.9.6]
    at org.apache.storm.netty.channel.Channels.write(Channels.java:671) ~[storm-core-0.9.6.jar:0.9.6]
    at org.apache.storm.netty.channel.AbstractChannel.write(AbstractChannel.java:248) ~[storm-core-0.9.6.jar:0.9.6]
    at backtype.storm.messaging.netty.Client.flushMessages(Client.java:323) ~[storm-core-0.9.6.jar:0.9.6]
    at backtype.storm.messaging.netty.Client.send(Client.java:250) ~[storm-core-0.9.6.jar:0.9.6]
    at backtype.storm.utils.TransferDrainer.send(TransferDrainer.java:54) ~[storm-core-0.9.6.jar:0.9.6]
    at backtype.storm.daemon.worker$mk_transfer_tuples_handler$fn__4610$fn__4611.invoke(worker.clj:338) ~[storm-core-0.9.6.jar:0.9.6]
    at backtype.storm.daemon.worker$mk_transfer_tuples_handler$fn__4610.invoke(worker.clj:336) ~[storm-core-0.9.6.jar:0.9.6]
    at backtype.storm.disruptor$clojure_handler$reify__1445.onEvent(disruptor.clj:58) ~[storm-core-0.9.6.jar:0.9.6]
    at backtype.storm.utils.DisruptorQueue.consumeBatchToCursor(DisruptorQueue.java:132) ~[storm-core-0.9.6.jar:0.9.6]
    at backtype.storm.utils.DisruptorQueue.consumeBatchWhenAvailable(DisruptorQueue.java:106) ~[storm-core-0.9.6.jar:0.9.6]
    at backtype.storm.disruptor$consume_batch_when_available.invoke(disruptor.clj:80) ~[storm-core-0.9.6.jar:0.9.6]
    at backtype.storm.disruptor$consume_loop_STAR_$fn__1458.invoke(disruptor.clj:94) ~[storm-core-0.9.6.jar:0.9.6]
    at backtype.storm.util$async_loop$fn__460.invoke(util.clj:463) ~[storm-core-0.9.6.jar:0.9.6]
    at clojure.lang.AFn.run(AFn.java:24) ~[clojure-1.5.1.jar:na]
    at java.lang.Thread.run(Thread.java:745) [na:1.7.0_67]
2015-12-15T21:38:37.516+0800 b.s.m.n.Client [ERROR] failed to send 945 messages to Netty-Client-eda.local/192.168.137.2:6708: java.nio.channels.ClosedChannelException
2015-12-15T21:38:37.521+0800 b.s.m.n.Client [ERROR] failed to send 944 messages to Netty-Client-eda.local/192.168.137.2:6708: java.nio.channels.ClosedChannelException
  

在删除指定文档中的指定区域之前调用。

替换():

#topology.skip.missing.kryo.registrations: false
storm.messaging.netty.flush.check.interval.ms: 100
storm.messaging.transport: "backtype.storm.messaging.netty.Context"
storm.messaging.netty.server_worker_threads: 1
storm.messaging.netty.client_worker_threads: 1
#storm.messaging.netty.buffer_size: 5242880
storm.messaging.netty.transfer.batch.size: 462144
storm.messaging.netty.buffer_size: 5242880
storm.messaging.netty.max_retries: 100
storm.messaging.netty.max_wait_ms: 1000
storm.messaging.netty.min_wait_ms: 100
topology.backpressure.enable: false
#storm.scheduler: "backtype.storm.scheduler.DefaultScheduler"
worker.childopts: "-Xmx768m -XX:NewSize=128m -XX:MaxNewSize=128m -XX:PermSize=64m -XX:MaxPermSize=64m -XX:MaxDirectMemorySize=128m -Djava.net.preferIPv4Stack=true  -XX:+UseParNewGC  -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -verbose:gc -Xloggc:/ffcs/gc/gc-storm-worker-%ID%.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/ffcs/gc/heapdump"
#worker.childopts: "-Xmx1024m"
supervisor.childopts: "-Xmx256m"
nimbus.childopts: "-Xmx768m"
ui.childopts: "-Xmx768m"
nimbus.thrift.threads: 256
  

在替换指定文档中的文本区域之前调用。

InsertString()方法似乎用于编辑文档。从我以往的经验来看,要更改JTextField中的文本,您需要使用类似this示例的DocumentListener。