Frage

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... cursor flashing white And the cursor flashing black. 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 highlighted area 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.

War es hilfreich?

Lösung

On my Mac with JDK7, I only get the annoying caret behavior with your code. I did not see the dot.

Switching from a MaskFormatter to a Format seems to solve this issue. My personal experience with JFormattedTextFields was always in combination with a Format which seems to work (after some little tweaks, see this post).

Anyway, here an adjusted version of your SSCCE which uses a Format

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;
      }
    };
  }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top