I have a JComboBox that is set to editable and has a keylistener attached. The input comes from a scan gun. What I am trying to do is if there are more than one item in the JComboBox, I need to scan a barcode that represents the # sign and it will scroll to the next item. The problem is that when I scan the # sign barcode, it scrolls which is good, but then it places the # sign at the end of the item text in the JComboBox. So say the item text was "1234", it ends up being "1234#". How do I keep the # sign from showing up? In VB I would just set the keycode = 0. I tried to do a e.setKeyCode(0); but didn't work.
private JComboBox getFromComboBox() {
if (fromComboBox == null) {
fromComboBox = new JComboBox();
fromComboBox.setEditable(true);
fromComboBox.setBackground(Color.white);
fromComboBox.setFont(new Font("Arial", Font.BOLD, 52));
fromComboBox.setModel(new DefaultComboBoxModel(new Object[] { "" }));
fromComboBox.setDoubleBuffered(false);
fromComboBox.setBorder(null);
fromComboBox.getEditor().getEditorComponent().
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent event) {
fromComboBoxKeyKeyPressed(event);
}
});
fromComboBox.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent event) {
fromComboBoxFocusFocusGained(event);
}
public void focusLost(FocusEvent event) {
fromComboBoxFocusFocusLost(event);
}
});
}
return fromComboBox;
}
private void fromComboBoxKeyKeyPressed(KeyEvent e) {
int num = fromComboBox.getItemCount();
if (e.getKeyCode() == 10) {
//enter key
if (WarehouseTracer.bScroll.equals(true)) {
fromComboBox.requestFocus();
WarehouseTracer.bScroll = false;
}else {
FromInput();
}
}else if ((e.getKeyCode() == 51) && (e.isShiftDown())){
//use # to scroll through From dropdown
WarehouseTracer.bScroll = true;
if (fromComboBox.getItemAt(num - 1).equals(fromComboBox.getSelectedItem())) {
fromComboBox.setSelectedIndex(0);
}else{
e.setKeyCode(0);
fromComboBox.setSelectedIndex(fromComboBox.getSelectedIndex() + 1);
}
}else if (e.getKeyCode() == 62) {
toText.requestFocus();
}
}
Thanks!