Java Combobox, Managing 2 fields from database

谁都会走 提交于 2019-12-25 06:33:58

问题


I want to get a result set with 2 fields from my DB.

 rs=Con.sqlQueryTable("Select id_prov, name_prov from prov");

And then I want to show the field called "name_prov" in the comboBox (As the item). But I also want to have my "id_prov" which is the ID (PRIMARY KEY) as the value for this item. This serves for the purpose of relating the name (of the providers in this case) with its ID just by using the combobox.

This is the code of the JComboBox event FocusGained that Im currently using.

try {
        //CBProv is the Combobox
        CBProv.removeAllItems();


        rs=Con.sqlQueryTable("Select id_prov, name_prov from prov");

        while(rs.next())
        {

            CBProvedores.addItem(rs.getString("name_prov"));
            //Here should be the Value related to the item I am creating



        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Error" + e  );
    }

Is there anyway I can accomplish this?


回答1:


First create a POJO, which will hold both your name and id.

public class ComboItem {
    private String id;
    private String name;

    public ComboItem(String id, String name) {
        this.id = id;
        this.name = name;
    }

    // Add the getter and setter as you want.

    // This will be used internally by JComboBox as the label to be displayed.
    @Override
    public String toString() {
        return name;
    }
}

Then use this POJO objects to be put into your JComboBox.

try {
        //CBProv is the Combobox
        CBProv.removeAllItems();

        rs=Con.sqlQueryTable("Select id_prov, name_prov from prov");

        while(rs.next())
        {                
            String id = rs.getString("id_prov"); // Get the Id
            String name = rs.getString("name_prov"); // Get the Name

            ComboItem comboItem = new ComboItem(id, name); // Create a new ComboItem
            CBProv.addItem(comboItem); // Put it into the ComboBox

        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Error" + e  );
    }

And then finally to get the value selected, you can do this:-

CBProv.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                ComboItem comboItem = (ComboItem) CBProv.getSelectedItem();
                // comboItem.getId(), comboItem.getName() - Use as you wish.
            }
    });



回答2:


Hi there i am Also still a newbie to java and javafx. This is what i did in javafx and it worked for me Hope you can work around it in java.

  private void fillProviders()
  {
    List<String> providerList = new ArrayList<String>();

  try
    {   
        String Sql = "select * from prov ";
        pat= conn.prepareStatement(Sql);
        rs=pat.executeQuery();

        while (rs.next())
        {
            providerList.add(rs.getString("id_prov")+" "+ rs.getString("name_prov"));
        }

        ObservableList<String> provider =  FXCollections.observableArrayList(providerList);
            bankName.setItems(provider);  
    }

    catch( Exception e)
    {
        JOptionPane.showMessageDialog(null, e);
    }         
}

Hope it works for you. Note that my my combobox Name is bankName



来源:https://stackoverflow.com/questions/15332113/java-combobox-managing-2-fields-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!