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:

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

I only have a moment here before I head out, but two options come to mind:
- Make the combo uneditable? Is the user really needing to edit entries?
- Install a DocumentListener on the combo box editor component that discards the '>'

From my understanding the combobox must be editable to have a KeyListener on it. I need the keylistener because the app only uses scan guns to capture keystrokes (no keyboard or mouse is accesssible).

I am unfamiliar w/ the DocumentListener but will look into it. Any more info you can provide?

Thanks.

From my understanding the combobox must be editable to have a KeyListener on it. I need the keylistener because the app only uses scan guns to capture keystrokes (no keyboard or mouse is accesssible).

Have you tried it? My personal experience from many years of Swing programming is that the only way to negotiate the nuances is to keep trying until it works :)

I am unfamiliar w/ the DocumentListener but will look into it. Any more info you can provide?

Thanks.

The DocumentListener interface gives you control over how changes to the content of a text component are processed. The default combo box editor is basically a JTextField which uses a Document interface to encapsulate its content.

It seems to work fine if you make the combo box uneditable and add the key listener directly to the JComboBox component instead of the editor

fromComboBox.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent event) {
            fromComboBoxKeyKeyPressed(event);
    }
});

Thanks for the input. I had to keep the combobox editable as a user could change the text via another barcode. I ended up getting an answer.

Thanks again for your input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.