こんにちは、ここで少し助けが必要です。データベースを接続してそれらの項目を JComboBox に表示することができましたが、現時点での苦労は、JComboBox の項目を変更するたびに、JTextField に表示される項目が常に JComboBox の最初の項目になることです。 JComboBox の 2 番目または 3 番目の項目をクリックします
public void JComboBoxToJTextFlied()
{
String dataSource = "CheckWriterDB";
String dbURL = "jdbc:odbc:" _ dataSource;
String temp = (String)listOfSuppliers.getSelectedItem();
String sql = (select Suppliers from SuppliersTable where Suppliers=?)
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connect= DriverManager.getConnection(dbURL, "", "");
PrepareStatement pst = connect.prepareStatement(sql);
pst.setString(1, temp);
resultSet result = pst.executeQuery();
//My Action perform so that everytime i change the item in my JComboBox
the new item will be shown in my JTextField
listOfSuppliersCombo.addItemListener(new ItemListener()
public void itemStateChange(ItemEvent event)
{
If(event.getStateChange() == ItemEvent.SELECTED)
{
try
{
if(result.next())
{
String add = result.getString("Suppliers")
newSuppliersEntryField.setText(add);
}
}
catch()
{
system.out.println("Your error is: " + e)
}
}
}
);
}
catch(Exception e)
{
System.out.println("Your error is: " + e)
}
}
注: listOfSupplierCombo は私の JComboBox であり、newSuppliersEntryField は私の JTextField です。
JcomboBox の項目を変更するたびに JTextField に同じ項目が表示されるようにコードを改善するにはどうすればよいですか? JcomboBox の ITem を変更するたびに、Jtext フィールドに表示されるアイテムは、Jcombobox で 2 番目、3 番目、4 番目などを選択した場合でも、常にコンボ ボックスの最初のアイテムになります。どうもありがとう。