Java 更新jtextfield';来自jtable
有人能帮我做这个听众吗Java 更新jtextfield';来自jtable,java,swing,jtable,listener,listselectionlistener,Java,Swing,Jtable,Listener,Listselectionlistener,有人能帮我做这个听众吗 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){ public void valueChanged(ListSelectionEvent e){ if(e.getValueIsAdjusting()){
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
if(e.getValueIsAdjusting()){
ListSelectionModel model = table.getSelectionModel();
int lead = model.getLeadSelectionIndex();
displayRowValues(lead);
}
}
private void displayRowValues(int rowIndex){
String country = "";
Object oCountry = table.getValueAt(rowIndex, 0);
country += oCountry.toString();
countryTxt.setText(country );
}
});
当选择一行时,它应该将数据从jtable(table)中的单元格发送到文本字段(countryTxt),但它只在我单击行时起作用,而不是在我用箭头键在表格中循环时起作用。问题在于这一行:
if (e.getValueIsAdjusting()) {
将其替换为:
if (e.getValueIsAdjusting()) return;
这是对多个选择事件的检查。问题在于这行:
if (e.getValueIsAdjusting()) {
将其替换为:
if (e.getValueIsAdjusting()) return;
顺便说一句,这是对多个选择事件的检查。如果您注释掉
e.getValueIsAdjusting()
,它会起作用
如果您注释掉
e.getValueIsAdjusting()
它会工作
很高兴我能提供帮助,不过你应该按照@Reimeus的建议去做,而不是完全注释出来,如果是真的,就返回,查看链接以获得解释这两种方法都有效,但我还有另一个问题:当删除所有行时,它会引发以下异常:java.lang.ArrayIndexOutOfBoundsException:1>=1我使用此方法删除所有行:DefaultTableModel=(DefaultTableModel)table.getModel();int rc=model.getRowCount();对于(int i=0;iGlad我可以帮忙,但是你应该按照@Reimeus的建议去做,而不是全部注释掉它,只要在为true时返回,查看链接以获得解释这两个方法都在工作,但我有另一个问题:当删除所有行时,它会引发此异常:java.lang.ArrayIndexOutOfBoundsException:1>=1我使用此方法删除所有行:DefaultTableModel=(DefaultTableModel)table.getModel();int rc=model.getRowCount();for(int i=0;i)