2012-07-24 105 views
1

有人可以幫助我與這位聽衆?更新jtextfield從jtable

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中(表)發送數據到一個文本框(countryTxt)當行的一個選擇,但是當我點擊行和它的作品不僅沒有當我騎自行車低谷我的桌子上有箭頭鍵。

回答

2
+0

你先生是魔術師!萬分感謝! – 2012-07-24 19:14:59

+1

很高興我能幫忙,儘管@Reimeus建議你應該做,而不是完全評論它,只要返回true時,請參閱鏈接的解釋 – Spiff 2012-07-24 19:17:52

+0

這兩種方法的工作,但我有另一個問題:當刪除所有行它引發了我這個異常:java.lang.ArrayIndexOutOfBoundsException:1> = 1 – 2012-07-24 19:31:00

5

問題是這一行:

if (e.getValueIsAdjusting()) { 

替換爲這樣:

if (e.getValueIsAdjusting()) return; 

這是BTW多個選擇事件的檢查。