When a user Scans a barcode representing a # sign while focus is on the fromComboBox, code is used to scroll through the list. It scrolls through the list fine, but then it moves to the next field on the screen, because the scanner (that is what we use for input) is setup to put a return character at the end of each scan (required). Is there a way to stop/cancel the return character from being read/processed? Below is a small sample program that demos the problem. You can run it and select one of the items in the combo. Then when I do a scan of the barcode to scroll, it scrolls, then moves to the next field (It should stay on the the combobox).
package testb;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
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 JButton getJButton0() {
if (okButton == null) {
okButton = new JButton();
okButton.setText("OK");
okButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
okButtonMouseMouseClicked(event);
}
});
}
return okButton;
}
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());
}
}
/**
* Main entry of the class.
* Note: This class is only created so that you can easily preview the result at runtime.
* It is not expected to be managed by the designer.
* You can modify it as you like.
*/
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("TestB");
frame.getContentPane().setPreferredSize(frame.getSize());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
//Here is the problem area. When a user Scans a barcode representing a # sign
//while focus is on the fromComboBox, this code is used to scroll through the
//list. It scrolls through the list fine, but then it moves to the next field
//on the screen. because the scanner (that is what we use for input) is setup
//to put a return character at the end of each scan (required). Is there a way
//to stop/cancel the return from being read?
private void fromComboBoxKeyKeyPressed(KeyEvent e) {
final int num = fromComboBox.getItemCount();
Boolean bScroll = new Boolean(false);
//this part runs after the scroll
if (e.getKeyCode() == 10) {
//enter key
if (bScroll.equals(true)) {
fromComboBox.requestFocus();
bScroll = false;
}else {
jTextField0.requestFocus();
}
}else if ((e.getKeyCode() == 51) && (e.isShiftDown())){
//use # to scroll through From dropdown
//runs through here fine
bScroll = true;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (fromComboBox.getItemAt(num - 1).equals(fromComboBox.getSelectedItem())) {
fromComboBox.setSelectedIndex(0);
}else{
fromComboBox.setSelectedIndex(fromComboBox.getSelectedIndex() + 1);
}
}
});
}
}
}
Thanks!!!!