简体   繁体   English

JFormattedTextField是在焦点上添加字符

[英]JFormattedTextField's is add characters on focus

I have a JFormattedTextField being initialized with the code 我有一个用代码初始化的JFormattedTextField

JFormattedTextField f = new JFormattedTextField(createFormatter());
f.setValue(null);
f.setColumns(1);
f.setEditable(true);
f.setCaretPosition(0);

The textField is 1 column wide and in the createFormatter method I have textField为1栏宽,在createFormatter方法中,

MaskFormatter formatter = null;
    try {
        formatter = new MaskFormatter("#");
        formatter.setValidCharacters("123456789");
    } catch (java.text.ParseException exc) {
        System.err.println("formatter is bad: " + exc.getMessage());
        System.exit(-1);
    }
    return (formatter);

As you can see I just want to numbers 1-9 in the text field. 如您所见,我只想在文本字段中输入1-9。 This works fine when tabbing to the text field but when I actually click on the text field to type I get this weird thing happening with the cursor. 切换到文本字段时,这很好用,但是当我实际单击文本字段以键入时,我发现光标发生了这种奇怪的事情。

Here the cursor is flashing blank... 此处光标闪烁空白... 光标闪烁白色 And the cursor flashing black. 并且光标闪烁黑色。 光标闪烁黑色

As you can see the there is a little black dot in the top left corner and the cursor moves away from the left side. 如您所见,在左上角有一个小黑点,光标从左侧移开。 I can highlight the area to the left 我可以突出显示左侧的区域 高亮区域 and cannot add any more characters to this text field (not even numbers 1-9). 并且不能在此文本字段中添加更多字符(甚至不能添加数字1-9)。 This makes me believe that when I focus on the text field with the cursor a character is being added. 这使我相信,当我用光标关注文本字段时,会添加一个字符。 I do not know what this character is and I do not know how to fix this. 我不知道这个字符是什么,也不知道如何解决。

Does anyone know how this can be fixed? 有谁知道如何解决?

Here is a sscce 这里是一个SSCCE

public class FormattedTextField {

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(100, 75);
    JPanel content = new JPanel(new FlowLayout());
    frame.setContentPane(content);

    JFormattedTextField f1 = new JFormattedTextField(createFormatter());
    f1.setValue(null);
    f1.setColumns(1);

    JFormattedTextField f2 = new JFormattedTextField(createFormatter());
    f2.setValue(null);
    f2.setColumns(1);

    content.add(f1);
    content.add(f2);

    frame.setVisible(true);
}

public static MaskFormatter createFormatter() {
    MaskFormatter formatter = null;
    try {
        formatter = new MaskFormatter("#");
        formatter.setValidCharacters("123456789");
    } catch (java.text.ParseException exc) {
        System.err.println("formatter is bad: " + exc.getMessage());
        System.exit(-1);
    }
    return (formatter);
}

} }

In this example There is not a little black dot in the top left corner but when the text field is focused on with the mouse, a blank character is still added. 在此示例中,左上角没有一个小黑点,但是当使用鼠标将文本字段聚焦时,仍会添加一个空白字符。

On my Mac with JDK7, I only get the annoying caret behavior with your code. 在装有JDK7的Mac上,您的代码只会出现烦人的插入符号行为。 I did not see the dot. 我没有看到圆点。

Switching from a MaskFormatter to a Format seems to solve this issue. MaskFormatter切换到Format似乎可以解决此问题。 My personal experience with JFormattedTextField s was always in combination with a Format which seems to work (after some little tweaks, see this post ). 我对JFormattedTextField的个人经验始终与似乎有效的Format结合使用(经过一些细微调整后,请参阅此文章 )。

Anyway, here an adjusted version of your SSCCE which uses a Format 无论如何,这是使用Format SSCCE的调整版本

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;
import java.awt.FlowLayout;
import java.text.AttributedCharacterIterator;
import java.text.FieldPosition;
import java.text.Format;
import java.text.NumberFormat;
import java.text.ParsePosition;

public class FormattedTextFieldDemo {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(100, 75);
    JPanel content = new JPanel(new FlowLayout());
    frame.setContentPane(content);

    JFormattedTextField f1 = new JFormattedTextField(createFormat());
    f1.setValue(null);
    f1.setColumns(1);

    JFormattedTextField f2 = new JFormattedTextField(createFormat());
    f2.setValue(null);
    f2.setColumns(1);

    content.add(f1);
    content.add(f2);

    JFormattedTextField f3 = new JFormattedTextField(createFormatter());
    f3.setValue(null);
    f3.setColumns( 1 );

    JFormattedTextField f4 = new JFormattedTextField(createFormatter());
    f4.setValue(null);
    f4.setColumns(1);

    content.add(f3);
    content.add(f4);

    frame.setVisible(true);
  }

  private static MaskFormatter createFormatter() {
    MaskFormatter formatter = null;
    try {
      formatter = new MaskFormatter("#");
      formatter.setValidCharacters("123456789");
    } catch (java.text.ParseException exc) {
      System.err.println("formatter is bad: " + exc.getMessage());
      System.exit(-1);
    }
    return (formatter);
  }

  private static Format createFormat(){
    final NumberFormat format = NumberFormat.getInstance();
    format.setParseIntegerOnly( true );
    return new Format() {
      @Override
      public StringBuffer format( Object obj, StringBuffer toAppendTo, FieldPosition pos ) {
        return format.format( obj, toAppendTo, pos );
      }

      @Override
      public AttributedCharacterIterator formatToCharacterIterator( Object obj ) {
        return format.formatToCharacterIterator( obj );
      }


      @Override
      public Object parseObject( String source, ParsePosition pos ) {
        int initialIndex = pos.getIndex();
        Object result = format.parseObject( source, pos );
        if ( result != null && pos.getIndex() > initialIndex + 1 ) {
          int errorIndex = initialIndex + 1;
          pos.setIndex( initialIndex );
          pos.setErrorIndex( errorIndex );
          return null;
        }
        return result;
      }
    };
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JFormattedTextField 插入符号在焦点上的位置 - JFormattedTextField caret position on focus 限制JFormattedTextField中的字符 - Limit characters in a JFormattedTextField 如何在JFormattedTextField中禁止字符? - How to disallow characters in JFormattedTextField? 是否可以在JFormattedTextField中添加ActionListener? - Is it Possible to Add ActionListener in the JFormattedTextField? jFormattedTextField的Formatter.setCommitsOnValidEdit(true)在第一次焦点时不起作用 - jFormattedTextField's Formatter.setCommitsOnValidEdit(true) doesn't work at first focus Swing:为什么 JFormattedTextField 在焦点丢失时添加“,” - Swing: why does JFormattedTextField adds “,” on focus lost 无法让 JFormattedTextField 在鼠标单击焦点事件上突出显示 - Unable to get JFormattedTextField to highlight on mouse click focus event 在我的代码示例中,如何从 JFormattedTextField 中删除焦点侦听器? - How to remove Focus Listener from the JFormattedTextField, in my example of the code? 获得焦点时如何 select JFormattedTextField 中的所有文本? - How to select all text in a JFormattedTextField when it gets focus? 如何在Java中的MaskFormatter的JFormattedTextField中的特定索引处插入字符? - how to insert characters at particular index in JFormattedTextField of MaskFormatter in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM