简体   繁体   English

选择行时将jTable的值传递到jComboBox

[英]Pass values of jTable into jComboBox when row selected

I am trying to pass data of selectedRow from jTable into jTextField and into jComboBox. 我试图将selectedRow的数据从jTable传递到jTextField和jComboBox。

For jTextField every row I select it automatically fills the jTextFields but for jComboBox part when I select a row, it fills correctly but when I select another row it sticks with the first selected data and I want to pass data of any selected row: 对于jTextField我选择的每一行,它会自动填充jTextFields,但是对于jComboBox部分,当我选择一行时,它会正确填充,但是当我选择另一行时,它将保留第一个选定的数据,并且我希望传递任何选定行的数据:

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
    // TODO add your handling code here:
   int SelectedRowIndex = jTable1.getSelectedRow();  
   String sid = (String) jTable1.getValueAt(SelectedRowIndex, 1);
   jComboBox2.addItem(sid);
   jTextField1.setText((String) jTable1.getModel().getValueAt(SelectedRowIndex, 2));
   jTextField3.setText((String) jTable1.getModel().getValueAt(SelectedRowIndex, 3));
   jTextField2.setText((String) jTable1.getModel().getValueAt(SelectedRowIndex, 12));  
} 

First of all variable names should NOT start with an upper case character. 首先,变量名不应以大写字母开头。 Some of your variable names are correct and some are not. 您的某些变量名正确,而某些则不正确。 Learn the Java conventions and use them properly. 学习Java约定并正确使用它们。

but when i select another row it sticks with the first selected data 但是当我选择另一行时,它会保留第一个选定的数据

I would guess that the combobox is empty when you start. 我猜想当您启动时组合框是空的。 Then when you add the first item, it gets selected automatically. 然后,当您添加第一个项目时,它会自动被选中。

But adding additional items does not change the selection. 但是添加其他项不会更改选择。

So I would suggest your code should be: 因此,我建议您的代码应为:

jComboBox2.addItem(sid);
jComboBox2.setSelectedIten(sid);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM