Reputation: 25
I created a class comboauto for to get an itemnames from mysql database in combo box. I did an auto suggestion in jcombobox assigned jtextfield as default, to get text in jtextfield. when i am typing a text U(it should auto suggest and give both lowercase and uppercase u values) in jcombobox. I am not getting both the case value when typing into a text. Please help me, what method i am supposed to use here.
This is the code i used
public class comboauto extends javax.swing.JFrame{
Vector Tmpv1=new Vector();
Statement TmpSqlStmnt;
ResultSet TmpSqlRs;
Connection Tmpcon;
String TmpCateId;
JTextField Txt01;
TreeMap TmpMap=new TreeMap();
public comboauto() {
initComponents();
}
public void FRM_CBX_AUTOSUGGEST() {
Txt01=(JTextField)Cbx01.getEditor().getEditorComponent();
Txt01.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e)
{
EventQueue.invokeLater(new Runnable()
{
public void run(){
String Tmptext =Txt01.getText();
if(Tmptext.length()==0)
{
Cbx01.hidePopup();
setModel(new DefaultComboBoxModel(Tmpv1), "");
}
else{
DefaultComboBoxModel m = getSuggestedModel(Tmpv1, Tmptext);
if(m.getSize()==0 ) {
Cbx01.hidePopup();
}
else{
setModel(m, Tmptext);
Cbx01.showPopup();
}
}
}
});
}
});
try{
Class.forName("com.mysql.jdbc.Driver");
Tmpcon =DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","root");
TmpSqlStmnt=Tmpcon.createStatement();
TmpSqlRs=TmpSqlStmnt.executeQuery ("SELECT CateId,CateName FROM Cust_Masterrecord ORDER BY CateName");
System.out.println("DB Connected");
TmpSqlRs.first();
do {
String TmpName=TmpSqlRs.getString("CateName");
Cbx01.setSelectedIndex(-1);
Cbx01.addItem(TmpName);
int TmpCateId=TmpSqlRs.getInt("CateId");
Tmpv1.addElement(TmpSqlRs.getString("CateName"));
TmpMap.put(TmpName, TmpCateId);
Cbx01.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == e.SELECTED) {
Lbl01.setText(TmpMap.get(Cbx01.getSelectedItem()).toString());
}
}
});
}while(TmpSqlRs.next());
}catch(Exception e){
System.out.println(e);
}
}
private boolean hide_flag = false;
private void setModel(DefaultComboBoxModel mdl, String str)
{
Cbx01.setModel(mdl);
Cbx01.setSelectedIndex(-1);
Txt01.setText(str);
}
private static DefaultComboBoxModel getSuggestedModel(java.util.List<String> list, String text) {
DefaultComboBoxModel m = new DefaultComboBoxModel();
for(String s: list) {
if(s.startsWith(text)) m.addElement(s);
}
return m;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
comboauto Tmpobj1= new comboauto();
Tmpobj1.FRM_CBX_AUTOSUGGEST();
Tmpobj1.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox Cbx01;
private javax.swing.JLabel Lbl01;
// End of variables declaration
}
Upvotes: 0
Views: 1041
Reputation: 485
maybe you could compare using
boolean equalsIgnoreCase(String anotherString) - Compares this String to another String, ignoring case considerations.
Upvotes: 1
Reputation: 4974
Use String.toUpperCase in you getSuggestedModel method. (Uppercase both strings. Lowercase also works.)
Upvotes: 1