Results 1 to 8 of 8
  1. #1
    cjmartin is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

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

  3. #3
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    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)
    Java 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);
    Alternatively, you could use a keylistener but i think its not recomended for some reason :S

    Hope this helps,
    berkeleybross

  4. #4
    cjmartin is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default

    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!

  5. #5
    cjmartin is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by cjmartin View Post
    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!
    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.

  7. #7
    cjmartin is offline Member
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default

    Thanks everyone. With your code examples I was able to fix my issue with supressing the keystrokes.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Glad you've got ti working! You'll want to mark this thread "Solved" in the thread tools at the top.

Similar Threads

  1. Different keycode values
    By dswastik in forum CLDC and MIDP
    Replies: 0
    Last Post: 01-29-2009, 04:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •