Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

ComboBoxModel / JComboBox question

  Asked By: Nisha    Date: Apr 08    Category: Java    Views: 1457
  

I'm using a ComboBoxModel to link a JComboBox with database.
I used a Vector that contains a series of Hashtables, one for each record of
database.

When I was writing "getSelectedItem" and "getElementAt" method I need to return
a String object (a field "assunto" contained in Hashtable), otherwise it will
show all Hashtable via toString() method. In this implementation, a duplicated
string will cause some inconsistences because I need to use the String (assunto)
to find the selected item at "setSelectedItem".

How do I index the JComboBox by a database primary key???


/***** THE CODE *****/

class TSelModel implements ComboBoxModel {
private int fSelected = 0;
private Vector fRecs = new Vector();

public TSelModel(){
db.query("SELECT id, assunto FROM solicitacao WHERE id_para = "+ fUserId);
Hashtable rs;
while((rs = db.nextRecord()) != null) {
fRecs.add(rs);
}
db.clean();
System.out.println(fRecs.size());
}

public int getSize(){
return fRecs.size();
}

public Object getElementAt(int index) {
Hashtable theCurrentRecord = (Hashtable) fRecs.elementAt(index);
String theValue = (String) theCurrentRecord.get("assunto");
return (Object) theValue;
}

public void addListDataListener(ListDataListener l) {
}

public void removeListDataListener(ListDataListener l) {
}

public void setSelectedItem(Object anItem) {
Hashtable theCurrentRecord;
String theValue;
for(int i=0;i<getSize();i++) {
theCurrentRecord = (Hashtable) fRecs.elementAt(i);
theValue = (String) theCurrentRecord.get("assunto");
if (theValue.equals(anItem)) {
fSelected = i;
break;
}
}
}

public Object getSelectedItem() {
Hashtable theCurrentRecord = (Hashtable) fRecs.elementAt(fSelected);
String value = (String) theCurrentRecord.get("assunto");
return (Object) value;
}
};

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on ComboBoxModel / JComboBox question Or get search suggestion and latest updates.