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
METHODCODE
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