我试过这个,但失败了。如果我从属性手动设置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());
答案 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);
}
}
};