JComboBox loading with KeyRelease

206 Views Asked by At

here my requirement is when I type something in combo box it should search the DB and get related data.when I'm typing it should fix to the exact data removing unwanted values..Here is my code..nothing happen

private void ComboItemName() {
    bbb = false;
    txtComboItemName = (JTextField) ComboItemName.getEditor().getEditorComponent();
    txtComboItemName.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent evt) {
            if (!(evt.getKeyCode() == KeyEvent.VK_DOWN || evt.getKeyCode() == KeyEvent.VK_UP || evt.getKeyCode() == KeyEvent.VK_LEFT || evt.getKeyCode() == KeyEvent.VK_RIGHT || evt.getKeyCode() == KeyEvent.VK_ENTER)) {
                try {
                    S3 = txtComboItemName.getText();
                    ResultSet rst = new JDBC.DB().getData("select * from item_reg where id like '%" + S3 + "'");
                    boolean b = rst.next();
                    boolean bb = false;
                    if (b) {
                        ComboItemName.removeAllItems();
                        bb = true;
                    }
                    while (b) {
                        if (rst.getString(1).startsWith(S3)) {
                            ComboItemName.addItem(rst.getString(1));
                        }
                        b = rst.next();
                    }
                    ComboItemName.setSelectedItem(S3);
                    txtComboItemName.setCaretPosition((ComboItemName.getSelectedItem() + "").length());
                    ComboItemName.showPopup();
                    int i = ComboItemName.getItemCount();

                    if (i > ComboItemName.getMaximumRowCount()) {
                        ComboItemName.setMaximumRowCount(15);
                    } else {
                        ComboItemName.setMaximumRowCount(i);
                    }
                    bbb = true;
                } catch (Exception ex) {
                    System.out.println(ex);
                }

            } else if (evt.getKeyCode() == KeyEvent.VK_ENTER && bbb == true) {

                boolean bIT = false;

                String Sr123 = (String) ComboItemName.getSelectedItem();
                try {
                    ResultSet Rst23 = new JDBC.DB().getData("select id from item_reg");
                    while (Rst23.next()) {
                        if (Sr123.equals(Rst23.getString(1))) {
                            bIT = true;
                            break;
                        } else {
                            bIT = false;
                        }
                    }
                    bbb = false;
                } catch (Exception ex) {
                    System.out.println(ex);
                }
            }
        }
    });
}
1

There are 1 best solutions below

21
On

is this ResultSet rst = new JDBC.DB().getData("select * from item_reg where id like '%" + S3 + "'"); query working fine ??

Check the value of b.

boolean b = rst.next();

May be your query is not working.