0

I want to create a virtual numeric keyboard, so that when I press U I get a 4, I produces a 5, O produces a 6, and so on:

789            789
uio   becomes  456  
jkl            123
m              0

But I need the rest of the keyboard to continue working as usual. I have tried this and some other solutions, but they are not useful to me, because on my JTextField I get 4U5I6O (or U4I5O6 depending on which solution I implement).

I need to get rid of the letter, and produce only the number.

Does anybody know a proper solution?

Thanks.

Community
  • 1
  • 1
ilvidel
  • 322
  • 1
  • 4
  • 13
  • Can't your keyboard event handler single out your letters, and act on them as if the number had been pressed? – Miquel Nov 28 '12 at 19:26

2 Answers2

3

If you are typing directly to JTextField, then I recommend to use DocumentFilter.

As an example of DocumentFilter, see:

Community
  • 1
  • 1
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • I tried the suggested solution, but the methods in the filter are never invoked, and thus, I get the original letters in my text. – ilvidel Nov 28 '12 at 20:16
  • 1
    @ilvidel Did you set the filter with `setDocumentFilter()`? – Eng.Fouad Nov 28 '12 at 20:18
  • 1
    @ilvidel If you tried this, post an SSCCE of this in your question. This approach should work, so most likely you are doing something wrong – Robin Nov 28 '12 at 22:56
  • @Eng.Fouad (and Robin) Ok, I was obviously doing something wrong. I did use `setDocumentFilter()` but I must have missed something else. Today, with a fresh mind I did it all over, and it worked. Thanks! – ilvidel Nov 29 '12 at 18:07
1

This is an eample of @Eng.Fouad's suggestion (please, all credit to him).

Basically, this will replace all the text entered into the text field with a random character instead.

I wouldn't be difficult to update the code to deliver mapped changes (for example a = 1) or even an encryption process.

public class TestPhasicDocumentFilter {

    public static void main(String[] args) {
        new TestPhasicDocumentFilter();
    }

    public TestPhasicDocumentFilter() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new PhasicPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PhasicPane extends JPanel {

        public PhasicPane() {

            setLayout(new GridBagLayout());
            JTextField field = new JTextField(12);
            add(field);

            ((AbstractDocument)field.getDocument()).setDocumentFilter(new PhasicDocumentFilter());

        }

        public class PhasicDocumentFilter extends DocumentFilter {
            protected String phasic(String text) {

                StringBuilder sb = new StringBuilder(text);
                for (int index = 0; index < sb.length(); index++) {
                    sb.setCharAt(index, (char)(33 + (int)Math.round(Math.random() * 93)));
                }

                return sb.toString();
            }

            @Override
            public void insertString(FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
                super.insertString(fb, offset, phasic(text), attr);
            }

            @Override
            public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                super.replace(fb, offset, length, phasic(text), attrs);
            }

        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366