3

My situation is that i have to scan a qrcode in a textfield that i have to parse that comes with non-printables characters (btw it's always the same character) that never comes in the same position.

I try to get this code with no success this

  • textfield.getText() failure
  • textfield.getDocument().getText(0,textfield.getDocument().getLength()) failure

I have success using KeyBindings and then replace as @trashgod comments with glyph.

Example:

public class Test {

    private JPanel panel;
    private JTextArea textArea;

    public static final String GS_UNICODE = "\u241D";

    public Test(){
        panel = new JPanel();
        textArea = new JTextArea(10,30);
        final JTextField textfield = new JTextField(30);

        textfield.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_F8,0), "test");
        textfield.getActionMap().put("test", new AbstractAction(){

            @Override
            public void actionPerformed(ActionEvent e) {
                textfield.setText(textfield.getText()+GS_UNICODE);                
            }

        });



        textfield.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {                
                    textArea.setText(textfield.getText());                                   
            }
        });
        panel.add(textfield);
        JButton button = new JButton("Press non-readable");
        button.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Robot robot = new Robot();
                    textfield.requestFocusInWindow();                    
                    robot.setAutoDelay(10);
                    robot.keyPress(KeyEvent.VK_F8);
                    robot.keyPress(KeyEvent.VK_H);
                    robot.keyPress(KeyEvent.VK_E);
                    robot.keyPress(KeyEvent.VK_L);
                    robot.keyPress(KeyEvent.VK_L);
                    robot.keyPress(KeyEvent.VK_O);
                    robot.keyPress(KeyEvent.VK_ENTER);  

                    SwingUtilities.invokeLater(new Runnable(){

                        @Override
                        public void run() {
                            //check if string has glyph
                           System.out.println(textfield.getText().contains(GS_UNICODE));
                           System.out.println(textfield.getText());
                        }

                    });

                } catch (AWTException ex) {
                    ex.printStackTrace();
                }

            }

        });
        panel.add(button);
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("DataMatrix example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        frame.setLocationByPlatform(Boolean.TRUE);
        Test example =new Test();
        frame.add(example.panel,BorderLayout.CENTER);
        frame.add(example.textArea,BorderLayout.SOUTH);

        //Display the window.
        frame.pack();
        frame.setVisible(Boolean.TRUE);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }


}

But my questions are:

1) Is there a way to capture non-printable ASCII codes (n<31 and n=127) via the underlying model of the textcomponent (a.k.a Document) instead of KeyBinding and not replacing with glyph like textfield.setText(textfield.getText()+"myReplacement");. For example in the model with the regex "\p{Cntrol}"

2) Assuming KeyBinding is the correct way , if i have to listen all non printable characters is there an automatic way of changing that code with its glyph?

nachokk
  • 14,363
  • 4
  • 24
  • 53
  • 1
    COuld you post such a String here? Which exactly chars can't you catch? – StanislavL Mar 28 '14 at 05:10
  • 2
    Are you looking for glyphs like these: [*Unicode Characters in the Control Pictures Block*](http://www.fileformat.info/info/unicode/block/control_pictures/images.htm)? – trashgod Mar 28 '14 at 05:42
  • the same I don't ****, :-) for better help sooner post an SSCCE/MCVE/MCTRE short, runnable, compilable with hardcoded value in local variable, btw (???I'm sure that???) @Andrew Thompson touched this issue in your posts and/or question – mKorbel Mar 28 '14 at 07:54
  • @trashgod i edit question to be more understable i hope, yes glyphs help a lot :) – nachokk Mar 28 '14 at 12:40
  • @mKorbel i post an example – nachokk Mar 28 '14 at 12:41
  • @StanislavL i want to know if i can catch in the text document , the non printable character, i edit question – nachokk Mar 28 '14 at 12:42
  • issue is that you are able to insert any unicode char by using combinations ALT + xxx (e.g. ALT +039 == '), then I see there only DocumentFilter as clear, simple, good solution – mKorbel Mar 28 '14 at 13:25
  • @mKorbel Im using a qr scanner, it's an automatic input. You mean that the documentFilter catch that inputs? BTW it's working but with keybindings i only want to know the 2 question i asked :P – nachokk Mar 28 '14 at 13:36
  • DocumentFIlter by default works in all cases programatically setText() , insert (one char from keyboars of sequesnce of chars from SystemClipBoard), delete (selected sequence of chars can be replaced with empty or one chars - or more) – mKorbel Mar 28 '14 at 13:57

0 Answers0