I have a code that goes like this in my java swing program:
JTextField textfield = (JTextField) txtNameID.getEditor().getEditorComponent();
textfield.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
comboFilter(textfield.getText());
}
});
}
});
It's function is to show names from database that's like what is being typed on the textfield. It should show up once the user started typing.
And this works well when using hard keyboard. But my problem is I have to use an on screen keyboard/virtual keyboard for this program and KeyListener doesn't work with it. Now I don't know what type of event listener I should use. I tried MouseListener but it doesn't work also. I hope there is a way. I really need this for my project. Please kindly help me out.
This is my code for the on-screen keyboard(on the same class, it has a longer code in a different class):
textfield.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
DialogVirtualKeyboardReal dlg = new DialogVirtualKeyboardReal(this, false, textfield);
dlg.setLocaleL(locale);
}
});
textfield.addMouseListener(new MouseListener() {
@Override
public void mousePressed(MouseEvent me) {
DialogVirtualKeyboardReal dlg = new DialogVirtualKeyboardReal(r, false, textfield);
dlg.setLocaleL(locale);
}
@Override
public void mouseReleased(MouseEvent me) {
}
@Override
public void mouseEntered(MouseEvent me) {
}
@Override
public void mouseExited(MouseEvent me) {
}
@Override
public void mouseClicked(MouseEvent me) {
}
});
Here's a picture of the DialogVirtualKeyboardReal:
The keys are made of JButtons.
EDITED: I tried DocumentListener following the instructions of @Abra.
The keyboard pops up, but it disappears after 1 letter typed in the textfield and the names below the textfield pops up.
It's good that the comboFilter appears even when I type from virtual keyboard. But I want it to execute simultaneously, while typing on the virtual keyboard, the comboFilter should appear.
Another thing is that it had an error with my database.
This is the code I created:
JTextField textfield = (JTextField) txtNameID.getEditor().getEditorComponent();
textfield.getDocument().addDocumentListener(new DocumentListener(){
@Override
public void insertUpdate(DocumentEvent de) {
filter();
}
@Override
public void removeUpdate(DocumentEvent de) {
filter();
}
@Override
public void changedUpdate(DocumentEvent de) {
filter();
}
public void filter(){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
comboFilter(textfield.getText());
}});
} });
And the error is:
errorcom.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
The comboFilter() code is:
public void comboFilter(String enteredText) {
List<String> filterArray= new ArrayList<String>();
String lname="";
String fname= "";
String mi= "";
String id= "";
try{
String str="SELECT * FROM patient_record WHERE firstname LIKE '"+enteredText+"%' OR lastname LIKE '"+enteredText+"%' OR patient_id LIKE '"+enteredText+"%'";
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/patient";
Connection con = DriverManager.getConnection(url,"root","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(str);
while(rs.next()){
lname=rs.getString("lastname");
fname= rs.getString("firstname");
mi= rs.getString("middlename");
id= rs.getString("patient_id");
String str1 = lname+", "+fname+" "+mi+". \t"+id;
filterArray.add(str1);
}}
catch(Exception ex){
System.out.println("error"+ex);
}
if (filterArray.size() > 0) {
jComboBox1.setModel(new DefaultComboBoxModel(filterArray.toArray()));
jComboBox1.setSelectedItem(enteredText);
jComboBox1.showPopup();
}
else {
jComboBox1.hidePopup();
}
}
I know the way I explained it is difficult to understand. Please bear with me. You guys may ask for further questions to better understand my question. Thanks a lot!