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?