-1

When a user presses the "dot" key on the keypad in a in a JTextField I'd like to transparently substitute it with a comma. I tried something like this:

jTextField.addKeyListener(new KeyAdapter() {
   @Override
   public void keyTyped(KeyEvent event) {
       if (event.getKeyCode() == KeyEvent.VK_DECIMAL) {
         event.setKeyChar(',');
       }
     }
});

but it doesn't work.

Justin
  • 6,611
  • 3
  • 36
  • 57
  • don't use low-level listeners in swing, instead use a documentFilter or JFormattedTextField. – kleopatra Jun 27 '14 at 14:19
  • As already stated `DocumentFilter` is the way to go for this situation. Hopefully this [answer](http://stackoverflow.com/a/9478124/1057230), be of some help on the topic :-) – nIcE cOw Jun 27 '14 at 15:32

4 Answers4

0

Instead of trying to associate a new key with an already happened key event (this is impossible I think), you should try directly manipulating the text of the related JTextField instance via a call like yourTextField.setText(","); in the if statement of your code snippet above.

Juvanis
  • 25,802
  • 5
  • 69
  • 87
0

The sane way of replacing input in a TextField is to use a DocumentFilder on the TextField's Document (http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html#filter).

Durandal
  • 19,919
  • 4
  • 36
  • 70
  • A `DocumentFilter` should be used for this. That is why the `DocumentFilter` API was added to the API. The point of the filter is to filter the text before the text is added to the Document. The Document invokes the filter so there is no need to reinvent the wheel by overriding the Document (this answer really deserves a down vote for that reason). Also the filter you create could potentially be reused in a text field or text pane even though one uses a PlainDocument and the other uses a StyledDocument. – camickr Jun 27 '14 at 15:12
  • @camickr Wasn't aware filter existed. Updated the answer. – Durandal Jun 27 '14 at 15:20
  • `Wasn't aware filter existed.` kleopatra made two comments on this topic before you posted your answer. Please read all answer/comments before posting. – camickr Jun 27 '14 at 15:32
0

Thank you all for your answers. It's true that the better choice would be to write a document filter, but I need to substitute the dot character only if is pressed on the numpad, not when it comes from the regular keyboard.

I know I could set a flag in the keylistener and than read it in the documentfilter, but it sounds a bit too convoluted.

Thank you very much.

Franco

0

Here is my solution:

public class MyTextField extends JTextField {
private boolean substituteDot;

public MyTextField() {
  super();
  addKeyListener(new KeyAdapter() {
    @Override
    public void keyPressed(KeyEvent event) {
      substituteDot = (event.getKeyCode() == KeyEvent.VK_DECIMAL);
    }
    @Override
    public void keyTyped(KeyEvent event) {
       if (substituteDot) {
         event.setKeyChar(',');
       }
     }
  });
 }
}

Thank you all!

Bye

Franco