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