Java如何从数据库中SetSelectedItem ComboBox?

时间:2016-03-25 13:07:53

标签: java netbeans combobox

我试过这个,但失败了。如果我从属性手动设置ComboBox,我可以将所选项目设置为该项目。

  Proto  Local Address          Foreign Address        State           PID
  TCP    0.0.0.0:80              0.0.0.0:0             LISTENING       1320 System
  UDP    0.0.0.0:1230            0.0.0.0:0             LISTENING       1320 sshd
  TCP    0.0.0.0:5000            0.0.0.0:0             LISTENING       1320  sshd
  UDP    0.0.0.0:139             0.0.0.0:0             LISTENING       1320 System

这个用于加载listComboBox

cbSetNoInvoice.setSelectedItem(txtSearchAll.getText());

Here is the screenshot. I want to set the selected item on the "Invoice" combobox if user input on the textfield and click the button

1 个答案:

答案 0 :(得分:0)

这样的东西? (我还没有测试过这个)

btnSearch.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String text = txtSearch.getText();
        int index = cbInvoice.getModel().getIndexOf(text);
        if (index == -1) {
            // the text is not already in the combobox
            cbInvoice.insertItemAt(text, 0);
            cbInvoice.setSelectedIndex(0);
        } else {
            // the text is already there
            cbInvoice.setSelectedIndex(0);
        }
    }
};