简体   繁体   English

Java Swing JTable数据收集在最后一个单元上失败

[英]Java Swing JTable data collection fails on last cell

I am trying to collect data from a JTable by using the method below to an array. 我试图通过使用下面的方法从一个JTable收集数据到数组。

public String[][] getTableData (JTable table) {
    DefaultTableModel dtm = (DefaultTableModel) table.getModel();
    int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();      
    String[][] tableData = new String[nRow][nCol];
    for (int i = 0 ; i < nRow ; i++)
        for (int j = 0 ; j < nCol ; j++)
            tableData[i][j] = (String) dtm.getValueAt(i,j);
    return tableData;
}

Everything works fine but I am not able to get data of the last row last column. 一切正常,但我无法获得最后一行最后一列的数据。 No error is given. 没有错误。 When I change the loop range, I get an arrayoutBound error. 当我改变循环范围时,我得到一个arrayoutBound错误。

The method that returns is below: 返回的方法如下:

public void setGrid(String data [][], String header []){
    dtm.setRowCount(0);
    dtm.setColumnCount(0);
    dtm.setDataVector(data, header);
    tbl1.getColumnModel().getColumn(0).setPreferredWidth(25);
}

I have edited the method like as below to get the data captured. 我已经编辑了如下所示的方法来获取数据。

public String[][] getTableData (JTable table) {
    String dat= new String();
    DefaultTableModel dtm = (DefaultTableModel) table.getModel();
    int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
    String[][] tableData = new String[nRow][nCol];
    for (int i = 0 ; i < nRow ; i++)
        for (int j = 0 ; j < nCol ; j++)
            dat=dat + (String) dtm.getValueAt(i,j);
            bb.Toplabel.setText("data is :" +dat);
            //tableData[i][j] = (String) dtm.getValueAt(i,j);
    return tableData;
}

The data displayed is null even though the cell has value. 即使单元格具有值,显示的数据也为空。

You had it almost right but forgot to store dat in the array. 你几乎没错,但忘记将数据存储在数组中。 Also casting object to the String doesn't make it String. 将对象强制转换为String也不会使它成为String。 Better way is to get String by toString() method or simply treat Object as String in other operator like plus(+). 更好的方法是通过toString()方法获取String,或者简单地将Object作为String添加到其他运算符中,例如加号(+)。 Here is right and simplified code: 这是正确和简化的代码:

public String[][] getTableData (JTable table) {
    DefaultTableModel dtm = (DefaultTableModel) table.getModel();
    int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
    String[][] tableData = new String[nRow][nCol];
    for (int i = 0 ; i < nRow ; i++)
        for (int j = 0 ; j < nCol ; j++) {
            String dat=""+ dtm.getValueAt(i,j);// this will be always the String
            bb.Toplabel.setText("data is :" +dat);
            tableData[i][j] = dat;
        }
    return tableData;
}

One more thing - always use curly braces in your code. 还有一件事 - 总是在代码中使用花括号。 Even for one line in the loop. 即使是循环中的一行。 It will help you avoid errors when you add more lines of the code to the loop body. 当您向循环体添加更多行代码时,它将帮助您避免错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM