Reputation: 33
Am use the following program for retrieve the value from my sql database to combobox.if i select first item in JComboBox from mysql database using ResultSet, it's not showing an id value of the first item name. if i selected second item in combobox(result set) it's showing the id value of the second selected item name. After i will click on first Item it's showing a first item Id value in label box. my problem is first item name is not showing ID value in label box, that means my first recordset value is not showing an id value?
package javacmbx;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class comboautosuggest extends javax.swing.JFrame {
Vector v=new Vector();
Vector v1=new Vector();
Statement TmpSqlStmnt;
ResultSet TmpSqlRs;
Connection con;
int CateId;
JTextField Txt01;
TreeMap Map=new TreeMap();
public comboautosuggest() {
initComponents();
}
public void AutoSuggest() {
Txt01=(JTextField)Cbx01.getEditor().getEditorComponent();
Txt01.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e)
{
EventQueue.invokeLater(new Runnable()
{
public void run(){
String text =Txt01.getText();//.toUpperCase();
if(text.length()==0)
{
Cbx01.hidePopup();
setModel(new DefaultComboBoxModel(v1), "");
}
else{
DefaultComboBoxModel m = getSuggestedModel(v1, text);
if(m.getSize()==0 ) {
Cbx01.hidePopup();
}
else{
setModel(m, text);
Cbx01.showPopup();
}
}
}
});
}
/*public void keyPressed(KeyEvent e)
{
String text = Txt01.getText();
int code = e.getKeyCode();
if(code==KeyEvent.VK_ENTER) {
if(!v1.contains(text)) {
v1.addElement(text);
}
hide_flag = true;
}else if(code==KeyEvent.VK_ESCAPE) {
hide_flag = true;
}else if(code==KeyEvent.VK_RIGHT) {
for(int i=0;i<v1.size();i++) {
String str = (String) v1.elementAt(i);
if(str.startsWith(text))
{
Txt01.setText(str);
return;
}
}
}
}*/
});
try{
Class.forName("com.mysql.jdbc.Driver");
con =DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","root");
TmpSqlStmnt=con.createStatement();
TmpSqlRs=TmpSqlStmnt.executeQuery ("SELECT location,pincode FROM combovalue ORDER BY location");
System.out.println("DB Connected");
TmpSqlRs.first();
do
{
String s= TmpSqlRs.getString("location");
Cbx01.addItem(s);
CateId=TmpSqlRs.getInt("pincode");
v1.addElement(s);
v.addElement(CateId);
Map.put(s, CateId);
Cbx01.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
Lbl01.setText(Map.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);
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;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Cbx01 = new javax.swing.JComboBox();
Lbl01 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Cbx01.setEditable(true);
Cbx01.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
Cbx01KeyPressed(evt);
}
public void keyTyped(java.awt.event.KeyEvent evt) {
Cbx01KeyTyped(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addComponent(Cbx01, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(50, 50, 50)
.addComponent(Lbl01, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(77, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(88, 88, 88)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Lbl01, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Cbx01, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(190, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void Cbx01KeyPressed(java.awt.event.KeyEvent evt) {
//Txt01=(JTextField)Cbx01.getEditor().getEditorComponent();
}
private void Cbx01KeyTyped(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
comboautosuggest Tmpcas=new comboautosuggest();
Tmpcas.AutoSuggest();
Tmpcas.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox Cbx01;
private javax.swing.JLabel Lbl01;
// End of variables declaration
}
Upvotes: 0
Views: 4616
Reputation: 39217
When you load the records from your database you are doing two things inside the while
-loop:
JComboBox
.ItemListener
to the JComboBox
.This has two implications: first you are adding multiple item listeners, namely one for each record. Whenever you change the selected value all of them will be triggered. Since they all perform the same action it is not necessary to declare more than one. Move the Cbx01.addItemListener(new ItemListener() { ... });
outside the loop.
Second you add the item listener after the first item itself. Therefore, adding the first value cannot trigger the listener at all and thus no id is shown (it will be show if you again select the first item using the mouse).
I suggest you move the addItemListener
before the loop. A good place would be where you instanciate the JComboBox
itself.
Upvotes: 2