所以,這個問題有兩個部分: -
事件處理程序在列被選中時執行。
獲取摘要的代碼。
對於第一個(事件處理程序),您可以參考@ Jeffrey的答案。對於摘要部分,你可以寫這樣的方法:
/* Method to return values in a column of JTable as an array */
public Object[] columnToArray(JTable table, int columnIndex){
// get the row count
int rowCount = table.getModel().getRowCount();
// declare the array
Object [] data = new Object[rowCount];
// fetch the data
for(int i = 0; i < rowCount; i++){
data[i] = table.getModel().getValueAt(i, columnIndex);
}
return(data);
}
調用此方法從這樣的事件處理中:
public void columnSelectionChanged(ListSelectionEvent e) {
//assuming single column is selected
Object[] data = columnToArray(table,table.getSelectedColumn());
/* type cast if using specific data type. for eg:
* Integer[] data = (Integer[]) columnToArray(table,table.getSelectedColumn());
*/
// other functions to create the summary
}
對象陣列可以被用來計算你需要的總結,如找到範圍,標準偏差等。這些應該是微不足道的。請記住在調用方法中對類型數組進行類型轉換。
你面臨的困難是什麼?它無法獲取列內容嗎?還是無法獲得被選中的列? – vaisakh 2012-04-07 19:10:22
@vaisakh:兩者都有。我假設我需要某種類型的偵聽器,但是我之前從未做過這種工作,通常會將其他人的代碼用於類似的項目(主要是因爲我發現javadoc不可讀/無用),但無法找到類似於調整的任何內容。 – screechOwl 2012-04-07 19:14:32