我有一個JTable,它是通過從文本文件中導入數據而形成的。這是一張巨大的桌子,大約有522列和數千行。此表中的許多單元格也是空的。 現在,我希望能夠將某些數學運算應用於某些列中的可用數據。所以現在,我可以選擇多個列,但我不知道如何去獲取這些值。我知道我需要一個數組數組,我可以存儲表列中的值,然後根據我的算法修改每個值。 現在,爲了簡單起見,我只想打印出select列中的值(其中一個簡單),但我不能這樣做,我得到了打印的特定單元格的值。 4次。我的測試代碼如下: 供選擇整個列,我使用此代碼:在JTable中修改整個列
public static void selectWholeColumn(final JTable table)
{
final JTableHeader header = table.getTableHeader();
header.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
int col = header.columnAtPoint(e.getPoint());
if(header.getCursor().getType() == Cursor.E_RESIZE_CURSOR)
{
e.consume();
}
else
{
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(false);
table.clearSelection();
table.setColumnSelectionInterval(col,col);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}
}
});
}
在我的GUI我有一個按鈕,當按下火災後端類和這個方法,它發生在一個JTable作爲參數執行打印出所有行的值選定列:
public void filterData(final JTable table)
{
TableModel model = table.getModel();
table.setCellSelectionEnabled(true);
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(false);
ListSelectionModel cellSelectionModel = table.getSelectionModel();
cellSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
cellSelectionModel.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
String selectedData = null;
int[] selectedRow = table.getSelectedRows();
int[] selectedColumns = table.getSelectedColumns();
for (int i = 0; i < selectedColumns.length; i++)
{
for (int j = 0; j < selectedRow.length; j++)
{
selectedData = (String) table.getValueAt(selectedRow[i], selectedColumns[j]);
}
}
System.out.println("Selected: " + selectedData);
}
});
任何建議,我怎麼能打印或基本上得到在選定的列或列的所有行的值,這樣我可以立即修改它們中的數據?
謝謝!