0

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);
                }
            }
        }
    });
}
JLink
  • 307
  • 1
  • 6
  • 15
  • It's likely the key event is being consumed, verify that keyReleased is actually being called – MadProgrammer Dec 06 '13 at 09:13
  • Use a DocumentListerner if you're only interested in knowing when new content is added to the field or a DocuemntFilter of you wish to filter what is going to the field – MadProgrammer Dec 06 '13 at 19:49

1 Answers1

0

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.

prime
  • 14,464
  • 14
  • 99
  • 131
  • when I add sout under that sql like this – JLink Dec 06 '13 at 09:15
  • String y = rst.getString(1); System.out.println(y); It returns this java.sql.SQLException: Illegal operation on empty result set what does this mean – JLink Dec 06 '13 at 09:16
  • Do this. Just after the `boolean b = rst.next();` line print the value of `b` and check it is `true` or `false` – prime Dec 06 '13 at 09:20
  • The `java.sql.SQLException: Illegal operation on empty result set` comes because your `rst` is empty. Check your query is working fine or not. – prime Dec 06 '13 at 09:21
  • then your result set `rset` is not empty. `String y = rst.getString(1); System.out.println(y);` should print the element. Since the result will depend on your text box value, check both are the same as the previous one (when you got the error `java.sql.SQLException: Illegal operation on empty result set` ) – prime Dec 06 '13 at 09:36
  • add the below code and check again. If b is true value should get printed. `System.out.println(b); String y = rst.getString(1); System.out.println(y);` – prime Dec 06 '13 at 09:45
  • it returns true.But when I erase combo box sometime it shows item ids.sometime a while loop is running without unstopping..I don't know what to do. – JLink Dec 06 '13 at 09:53
  • after it returns true by `System.out.println(b);` what will be print by `String y = rst.getString(1); System.out.println(y);` – prime Dec 06 '13 at 10:03
  • there is an item in my item_reg table hdd01 when I type that no respond but when I delete that character by character hdd01 true hdd01 true like that sout prints.when it is completely deleted all my item ids load to combo box – JLink Dec 06 '13 at 10:08
  • Try `public void keyTyped(KeyEvent evt);` instead of `public void keyReleased(KeyEvent evt);` and `when it is completely deleted all my item ids load to combo box` it is normal. – prime Dec 06 '13 at 10:29
  • same result there is a while loop running background so pc gets slow and struck – JLink Dec 06 '13 at 10:34
  • did you see the madprogrammers comment above he said key event consumed verify it – JLink Dec 06 '13 at 10:35
  • consumed means when you press the key it can be consumed by another event or something. If so the relevant listener will not respond. (Don't know much about it) – prime Dec 06 '13 at 10:45
  • to consume use this. `public void keyReleased(KeyEvent evt) { evt.consume(); ....` Check that. lets see what it will do. – prime Dec 06 '13 at 10:46
  • so what should I do..do you have any other way to do this.any code..? – JLink Dec 06 '13 at 10:49
  • `S3 = txtComboItemName.getText();` print S3 here and check the String is read fine. `ResultSet rst = new JDBC.DB().getData("select * from item_reg where id like '%" + S3 + "'");` – prime Dec 06 '13 at 10:51
  • did you check the modification `public void keyReleased(KeyEvent evt) { evt.consume(); ....` ? – prime Dec 06 '13 at 10:52
  • check [this](http://stackoverflow.com/questions/14218749/using-jcombobox-as-a-search-box) – prime Dec 06 '13 at 10:59
  • S3 get that value but there are more than 10 sout prints what that means – JLink Dec 06 '13 at 11:00
  • hmmm. Check the link i commented above. It contain a solution which i think suits for you. Try that out. – prime Dec 06 '13 at 11:07
  • access the DB is time consuming, If the DB is not changing for a while you can load the combo box once and use autocompletion. See [this](http://www.orbital-computer.de/JComboBox/) and [this](http://stackoverflow.com/questions/13681977/jcombobox-autocomplete) – prime Dec 06 '13 at 11:11