问题
I am trying to set up a combo box that will display a UserName, but when selected will send a an ID instead.
I have already set up a method that queries my Derby database and returns a multidimensional array with the UserNames and IDs.
I did some research on how to set this up and found several posts suggesting JFace, but nothing on how to accomplish this.
I have tried setData(String Key, Object Value), and add(String String, int index). I have also looked at DataBinding, but haven't found anything that I am able to make work.
Thanks,
Update---------------------- I got it working to a point. I think I have the content provider set up properly however when I go to run the app it prints all the labels the same as the last item in the list.
Model Provider:
public enum ModelProvider {
INSTANCE;
public List<Animal> Animals;
public String conn = "L:\\Dropbox\\Herd_Management\\database";
private ModelProvider() {
Animals = new ArrayList<Animal>();
String strSQL = null;
strSQL = "SELECT Herd_ID FROM dbherd_management.TBLHERD";
String[] straHerd_IDs = CC_Derby.getResultArray(conn, "", "", strSQL);
for (int r = 0; r < straHerd_IDs.length; r++) {
Animals.add(new Animal(Integer.parseInt(straHerd_IDs[r])));
}
}
public List<Animal> getAnimals() {
return Animals;
}
}
Snippet For Content and Label Providers:
comboViewer_9 = new ComboViewer(shlHerdManagement, SWT.NONE);
final Combo cboAnimalTag = comboViewer_9.getCombo();
cboAnimalTag.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
true, false, 1, 1));
cboAnimalTag.setText("<Select Animal Tag>");
comboViewer_9.setContentProvider(new ArrayContentProvider());
comboViewer_9.setInput(ModelProvider.INSTANCE.getAnimals());
comboViewer_9.setLabelProvider(new LabelProvider() {
@Override
public String getText(Object element) {
Animal a = (Animal) element;
return (a.getTag());
}
});
Animal Constructor
public Animal(int intAnimal_ID) {
Connection conn = CC_Derby.createConnection("L:\\Dropbox\\Herd_Management\\database","","");
String strSQL = "SELECT * FROM dbherd_management.TBLHERD WHERE HERD_ID ="
+ intAnimal_ID;
try {
stmt = conn.createStatement();
ResultSet results = null;
results = stmt.executeQuery(strSQL);
while (results.next()) {
intHerd_id = results.getInt(1);
strHerd_Tag_Letter = results.getString(2);
intHerd_Tag_Num = results.getInt(3);
strHerd_Tag_Color = results.getString(4);
strHerd_Sex = results.getString(5);
strHerd_Type = results.getString(6);
dtHerd_Birthdate = results.getString(7);
intHerd_Sire = results.getInt(8);
intHerd_Dam = results.getInt(9);
intHerd_Owner = results.getInt(10);
strHerd_TimeStamp = results.getString(11);
strHerd_Status = results.getString(12);
dtHerd_Status_Date = results.getString(13);
}
results.close();
stmt.close();
CC_Derby.shutdown(stmt, conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
getTag Method
public String getTag(){
String strResult = null;
String l = null;
int n = 0;
String c = null;
l = getHerd_Tag_Letter();
n = getHerd_Tag_Num();
c = getHerd_Tag_Color();
if(l == null){
l ="";
}
if(c == null){
c = "";
}
strResult = l + n + " " + c;
return strResult;
}
回答1:
You have a viewer with a content provider and a label provider. Content provider serves the objects (UserNameIdPair), label provider provides labels (UserName string). View's selection listeners will be "sent" objects provided by content provider.
来源:https://stackoverflow.com/questions/8594546/how-do-i-set-a-key-and-a-value-in-a-jface-comboviewer