Results 1 to 8 of 8
Thread: How can I supress keycode?
- 08-04-2010, 12:27 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 7
- Rep Power
- 0
How can I supress keycode?
For my application we use scan guns to scan barcodes to do various things. My issue is with tabbing. We scan a barcode that represents the ">" sign. When focus is on the combobox box and they scan the ">" sign it places the ">" in the combobox. How can I keep this from displaying? Is there a way to supress the keycode or some other method? I have used the e.consume and the SwingUtilities.invokeLater methods as they have worked in the past, but not working here. I have also tried to save the value of the item selected in the combobox and then selecting it again at the end based on the selectedindex.
Here is a demo of my issue:
Java Code:import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.UIManager; import org.dyno.visual.swing.layouts.Constraints; import org.dyno.visual.swing.layouts.GroupLayout; import org.dyno.visual.swing.layouts.Leading; //VS4E -- DO NOT REMOVE THIS LINE! public class TestB extends JFrame { private static final long serialVersionUID = 1L; private JComboBox fromComboBox; private JTextField jTextField0; //private JButton okButton; private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel"; public TestB() { initComponents(); } private void initComponents() { setLayout(new GroupLayout()); add(getJComboBox0(), new Constraints(new Leading(83, 148, 10, 10), new Leading(56, 10, 10))); add(getJTextField0(), new Constraints(new Leading(85, 136, 10, 10), new Leading(109, 29, 10, 10))); //add(getJButton0(), new Constraints(new Leading(105, 10, 10), new Leading(185, 10, 10))); setSize(320, 240); } private JTextField getJTextField0() { if (jTextField0 == null) { jTextField0 = new JTextField(); jTextField0.setText("jTextField0"); } return jTextField0; } private JComboBox getJComboBox0() { if (fromComboBox == null) { fromComboBox = new JComboBox(); fromComboBox.setEditable(true); fromComboBox.setModel(new DefaultComboBoxModel(new Object[] { "" })); fromComboBox.setDoubleBuffered(false); fromComboBox.setBorder(null); fromComboBox.addItem("oranges"); fromComboBox.addItem("lemons"); fromComboBox.addItem("limes"); fromComboBox.getEditor().getEditorComponent(). addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent event) { fromComboBoxKeyKeyPressed(event); } }); } return fromComboBox; } private static void installLnF() { try { String lnfClassname = PREFERRED_LOOK_AND_FEEL; if (lnfClassname == null) lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName(); UIManager.setLookAndFeel(lnfClassname); } catch (Exception e) { System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL + " on this platform:" + e.getMessage()); } } public static void main(String[] args) { installLnF(); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { TestB frame = new TestB(); frame.setDefaultCloseOperation(TestB.EXIT_ON_CLOSE); frame.setTitle("TestCancelKeyStroke"); frame.getContentPane().setPreferredSize(frame.getSize()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } private void fromComboBoxKeyKeyPressed(KeyEvent e) { final int num = fromComboBox.getItemCount(); Boolean bScroll = new Boolean(true); //This checks for the key that is scanned to Tab to the //next field. It is the > sign. if ((e.getKeyCode() == 46) && (e.isShiftDown())) { SwingUtilities.invokeLater(new Runnable() { public void run() { jTextField0.requestFocus(); } }); e.consume(); } } }
-
Hm, not sure if this will work every time, but one possible solution is to extract the document from the combo box's editor and use a document filter like so:
Java Code:private JComboBox getJComboBox0() { if (fromComboBox == null) { fromComboBox = new JComboBox(); fromComboBox.setEditable(true); fromComboBox.setModel(new DefaultComboBoxModel(new Object[]{""})); fromComboBox.setDoubleBuffered(false); fromComboBox.setBorder(null); fromComboBox.addItem("oranges"); fromComboBox.addItem("lemons"); fromComboBox.addItem("limes"); JTextField cEditor = (JTextField) fromComboBox.getEditor().getEditorComponent(); PlainDocument doc = (PlainDocument) cEditor.getDocument(); doc.setDocumentFilter(new DocumentFilter() { @Override public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { if (string.equals(">")) { jTextField0.requestFocus(); } else { super.insertString(fb, offset, string, attr); } } @Override public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { if (text.equals(">")) { jTextField0.requestFocus(); } else { super.replace(fb, offset, length, text, attrs); } } }); } return fromComboBox; }
- 08-04-2010, 01:05 PM #3
You may want to look at this tutorial
You should assign a key binding to each field, and you want the action to be field.transferFocus();
e.g. (code not tested)
Alternatively, you could use a keylistener but i think its not recomended for some reason :SJava Code:Action changeFocus = new AbstractAction() { public void actionPerformed(ActionEvent e) { comboBox.transferFocus(); } }; KeyStroke keyStroke = KeyStroke.getKeyStroke('>'); comboBox.getInputMap(JComponent.WHEN_FOCUSED).put(keyStroke, "changeFocus"); comboBox.getActionMap().put("changeFocus", changeFocus);
Hope this helps,
berkeleybross
- 08-05-2010, 12:11 AM #4
Member
- Join Date
- Aug 2010
- Posts
- 7
- Rep Power
- 0
Fubarable....this seems to be working for me. Thanks for the solution. Now this works for comboboxes. What about jtextfields? I am having the same problem where the scanned character is displaying in the field. Thanks!
- 08-05-2010, 12:17 AM #5
Member
- Join Date
- Aug 2010
- Posts
- 7
- Rep Power
- 0
Also to clarify, there are different characters that can be scanned to emulate different actions. Such as Tab and Backspace. And they can be on jtextfields or jcombobox.
-
Yes, of course it will work for JTextFields as this was adapted from a technique commonly used on JTextFields, the DocumentFilter. Again, you'll have to extract the JTextField's Document and cast it to a PlainDocument. For more details, please see the Sun tutorial on using DocumentFilters. Luck.
- 08-06-2010, 05:38 PM #7
Member
- Join Date
- Aug 2010
- Posts
- 7
- Rep Power
- 0
Thanks everyone. With your code examples I was able to fix my issue with supressing the keystrokes.
-
Similar Threads
-
Different keycode values
By dswastik in forum CLDC and MIDPReplies: 0Last Post: 01-29-2009, 03:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks