JTextField/JTextComponent 中的选择有限?
考虑一个 JFormattedTextField(或者任何 JTextComponent,实际上),其中在字段的实际“文本”周围显示有一个前缀和一个后缀。
例如,双 3.5 将是字符串(通过格式化)“3.50”,其周围将是前缀“$”和后缀“”,用于显示文本“$ 3.50”。
显然,这很容易做到。但是,用户仍然可以选择前缀/后缀内的文本,因此他们可以想象删除部分或全部前缀/后缀。我希望用户受到限制,以便根本无法选择前缀/后缀(同时仍然是文本字段的一部分,因此没有 JLabels)。我几乎可以使用 CaretListener(或通过覆盖 setCaretPosition/moveCaretPosition)来完成此操作,这可以防止 Ca 选择整个字段,并且可以防止使用箭头键移动到前缀/后缀。但是,鼠标拖动和 Shift 箭头键仍然允许将选择移动到这些限制区域。
有什么想法吗?
Consider a JFormattedTextField (or any JTextComponent, really) wherein there is a prefix and a suffix displayed around what is the actual "text" of the field.
For instance, the double 3.5 would be the String (via formatting) "3.50" around which would be the prefix "$ " and the suffix "", for a display text of "$ 3.50".
Clearly, this is simple to do. However, the user is still allowed to select text within the prefix/suffix, so they could conceivably delete part or all of the prefix/suffix. I would prefer the user be restricted such that the prefix/suffix cannot be selected at all (while still part of the text field, so no JLabels). I can almost accomplish this with a CaretListener (or by overriding setCaretPosition/moveCaretPosition), which prevents a C-a from selecting the entire field, and it prevents using the arrow keys to move into the prefix/suffix. However, mouse dragging and shift-arrow keys still allows the selection to move into these restricted areas.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为此使用 NavigationFilter。
下面是一个帮助您入门的示例:
我相信这就是 JFormattedTextField 的工作原理。所以我不确定您是否可以将其与格式化文本字段一起使用,因为它可能会替换默认行为。
You can use a NavigationFilter for this.
Here is an example to get you started:
I believe this is a how a JFormattedTextField works. So I'm not sure if you can use this with a formatted text field as is may replace the default behaviour.