2016-03-25 6 views
0

私はこれを試しましたが、失敗しました。プロパティからComboBoxを手動で設定すると、選択した項目をその項目に設定できます。Javaの場合、データベースからのコンボボックスをSetSelectedItemする方法は?

cbSetNoInvoice.setSelectedItem(txtSearchAll.getText()); 

とこの1つは、負荷のために、このようなlistComboBox

for (Transaction tr : listTransaction) { 
cbSetNoInvoice.addItem(tr.getNo_invoice()); 

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

+0

あなたはcomboBoxの値をデータベースの値にバインドしますか?ここで達成しようとしているものが明確でない –

+0

リストのtempのようなものなので、保存した後にjTextFieldの値がリストと同じであればremoveItemを実行できます。 それは私が選択した場合、2回表示されません... それはすべて私が欲しいです –

答えて

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); 
     } 
    } 
}; 
+0

はい、種類の、しかし、それはまだコンボボックスにはない...を挿入することはありませんが、 t cbInvoice.setSelectedを実行します。最初に追加されたとき、おそらくアイテムは文字列ではないでしょうか? –