如何在 JFace ComboViewer 中设置键和值?

发布于 2024-12-22 12:03:37 字数 3473 浏览 0 评论 0 原文

我正在尝试设置一个组合框来显示用户名,但选择时将发送一个 ID。

我已经设置了一个方法来查询我的 Derby 数据库并返回包含用户名和 ID 的多维数组。

我做了一些关于如何设置它的研究,并发现了几篇建议使用 JFace 的帖子,但没有任何关于如何实现这一点的文章。

我尝试过 setData(String Key, Object Value) 和 add(String String, int index)。 我也研究过 DataBinding,但没有找到任何可以工作的东西。

谢谢,

更新了---------------------- 我已经让它工作到一定程度了。 我认为我已经正确设置了内容提供程序,但是当我运行该应用程序时,它会打印与列表中最后一项相同的所有标签。

模型提供者:

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;
}

}

内容和标签提供者的片段:

            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());
                  }
                });

动物构造

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 方法

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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水染的天色ゝ 2024-12-29 12:03:37

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文