Welcome to Dream.In.Code
Become a Java Expert!

Join 149,577 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,739 people online right now. Registration is fast and FREE... Join Now!




Auto complete jTextField,Jlist,popupMenu

 
Reply to this topicStart new topic

Auto complete jTextField,Jlist,popupMenu, Auto complete

Half_Man_Code
11 Oct, 2007 - 12:11 AM
Post #1

New D.I.C Head
*

Joined: 23 Feb, 2006
Posts: 3


My Contributions
Hi,
I am stack trying to to add fuctionality to my application, this functionality entails the following

1) when a user starts typing in the textfield,the text typed is used to run a query that will fetch results then puts them in the jlist and the jList is added to the popup which then gets displayed( setVisible(true) ),everything works thus far,the only problem is that i can not get the down arrow keys and up keys to work so that you can go up and down the list displayed on the popup,its kinda like the google textField, or the auto complete you find in most IDEs

can someone help me out this is the code below,this is the method that i call on key release on my textfield

METHOD
CODE

public void showPopupResults()
    {
        
         jPopupMenu2.setVisible(false); //close other popups
         jPopupMenu3.setVisible(false); //close other popups
  
          
        final    DefaultListModel popupList = new  DefaultListModel();
        String   nameCheck = null;
        String pL=null;
        String pF=null;
        String master=null;
        jPopupMenu1.setLocation(181,236);//setting the location of the popup to be displayed
        String getName = jTextField2.getText();
                
          if(!"".equals(getName) && getName.length() >0) {

                String sqlNameSeach = "SELECT  DISTINCT name FROM client WHERE name LIKE '"+getName+"%' LIMIT 15 ";
                try {

                    this.resultSet = this.connection.executeQuery(sqlNameSeach);
                    while(this.resultSet.next()) {

                                 String nameV = jTextField2.getText();
                                 String name2=null;
                                 name2 = this.resultSet.getString("name");
                                 jPopupMenu1.setVisible(false);
                                 jTextField2.requestFocus();
                                 popupList.addElement(this.resultSet.getString("name"));
                                 master = this.resultSet.getString("name");
                                 pL = (String) popupList.lastElement();
                    }
                     jList4.setModel(popupList);    
                     jList4.setSelectedIndex(0);
                     jList4.requestFocus();
                     jList4.addKeyListener(new java.awt.event.KeyAdapter() {
                    
                         public void keyReleased(KeyEvent e)
                         {
                             int count=0;
                             int keycode = e.getKeyCode();
                             switch(keycode) {

                                case KeyEvent.VK_F1:
                                 //textdisplay.setText(textdisplay.getText() + "\nHelp Feature");
                                    System.out.println("\nHelp Feature");
                                break;
                                case KeyEvent.VK_UP:
                                //textdisplay.setText(textdisplay.getText() + "\nUp");
                                    System.out.println("\\nUp Feature");
                                break;
                                
                                case KeyEvent.VK_DOWN:
                                    
                                    count++;
                                    System.out.println("Press "+count);
                               // textdisplay.setText(textdisplay.getText() + "\nDown");
                                    System.out.println("\nDown Feature");

                                     jList4.setSelectedIndex(count);
                                    
                                break;
                                case KeyEvent.VK_LEFT:
                               // textdisplay.setText(textdisplay.getText() + "\nLeft");
                                    System.out.println("nLeft Feature");
                                break;
                                case KeyEvent.VK_RIGHT:
                               // textdisplay.setText(textdisplay.getText() + "\nRight");
                                    System.out.println("\nHelp Feature");
                                break;
                                }
                         }
                    
                     });
                    
                    // jList4.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), JComponent.WHEN_FOCUSED);

                  
                    jScrollPane3.add(jList4);
                    jPopupMenu1.add(jList4);
                
                  
                   if(!popupList.isEmpty()) {  
                        jPopupMenu1.setVisible(true);
                   }
                     if(jTextField2.getText().equalsIgnoreCase(pL))   //jTextField2 is the textfield that is typed into
                     {    
                          jPopupMenu1.setVisible(false);
                     }
                    if(popupList.isEmpty())
                    {
                        jPopupMenu1.setVisible(false);
                      
                    }
                }
        
                catch(SQLException e)
                {
                    e.printStackTrace(System.err);
                }
                    //mouse action  
                    MouseListener mouseListener = new MouseAdapter() {

                    public void mouseClicked(MouseEvent e) {
                        if (e.getClickCount() == 1)
                        {

                            int index = jList4.locationToIndex(e.getPoint());
                            String nameText = (String) jList4.getSelectedValue();
                            System.out.println("Double clicked on Item " + index+" "+nameText);
                            jTextField2.setText(nameText);
                            jPopupMenu1.setVisible(false);
                            popupList.clear();  
                            jTextField10.requestFocus();
                            
                        }
                    }

                };
                
                   MouseListener mouseListener2 = new MouseAdapter() {

                    public void mouseClicked(MouseEvent e) {
                        if (e.getClickCount() == 1)
                        {
                            jPopupMenu1.setVisible(false);
                          
                            
                        }
                    }

                };
                        jList4.addMouseListener(mouseListener);
                        jPanel1.addMouseListener(mouseListener2);
                        jList4.getAutoscrolls();
          }
          else
          {
                 popupList.clear();
                 jList4.setModel(new DefaultListModel());
                 jPopupMenu1.setVisible(false);
          }
        
        
        
    } //end of Method


*next time please use code tags, ty
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:35PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month