[英]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? 有谁知道如何解决?
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.