Question

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.

Was it helpful?

Solution

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

As an example of DocumentFilter, see:

OTHER TIPS

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);
            }

        }

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top