0
我寫了下面的代碼,使一個新的JTable時,我就當前JTable的選擇:從另一個JTable的選擇中創建新的JTable?
makeTbale.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
int[] selectedCols = table.getSelectedColumns();
Object[] newCols = new Object[selectedCols.length];
TableModel model = table.getModel();
int selectedRows = model.getRowCount();
Object[][] output = new Object[selectedCols][selectedRows];
for(int nCol = 0; nCol < selectedCols.length; nCol++) {
for{int nRow = 0; nRow<selectedRows;nRow++) {
output[nCol][nRow] = model.getValueAt(nRow, selectedCols[nCol]).toString();
newCols[nCol] = table.getModel().getColumnName(selectedCols[nCol]);
}
}
table.setModel(new DefaultTableModel(output,newCols));
});
現在,這是我選擇的數據:
Col2 |Col3 |Col5 |Col6 |Col7 |Col10
1/1/2017 |3.45 |1.2 |hello |b1_133 |test
1/2/2017 |3.11 |4.9 |x |b3_122 |you
1/3/2013 |2.98 |5.5 |ask |c1_001 |foo
1/4/2012 |1.90 |2.1 |y |v7_670 |hey
1/6/2000 |9.99 |7.77 |w |c3_890 |fee
1/7/2012 |10.45 |8.75 |test |u0_998 |faa
.
.
.
它的股價下跌至100更多的行。
這是我跑我的代碼後,即可獲取表:
Col2 |Col3 |Col5 |Col6 |Col7 |Col10
1/1/2017 |1/2/2017 |1/3/2013 |1/4/2012 |1/6/2000 |1/7/2012
3.45 |3.11 |2.98 |1.90 |9.99 |10.45
1.2 |4.9 |5.5 |2.1 |7.77 |8.75
hello |x |ask |y |w |test
.
.
.
你的想法,選中的列現在行和行是正被視爲列,只有6行(相當於列數?)。
我的代碼中的錯誤在哪裏?
這就是我想!但是當我這樣做的時候,我得到一個運行時錯誤,指出數組索引超出範圍:6 – Abdane
您也可能會使用for循環錯誤的方式,因爲您的行數多於列數,如果將它們混合在一起,那麼它會嘗試將更多的行放入列中,比列空間要多 – dahui
嘗試for循環到selectedCols.length - 1 – dahui