I have a JFormattedTextField being initialized with the code
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
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. 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). 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
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.