简体   繁体   中英

How to pass an 2D array to jtable in Netbeans

It is my first time I create a jtable,I want to display a jtable of int from another class.So I call the method getTable and assign it to the jtable,is it right?

jTable1 = new javax.swing.JTable();

jTable1.setModel(new javax.swing.table.DefaultTableModel(
   new int[][] = TableAdapter.getTableC()

));

jScrollPane1.setViewportView(jTable1);

It keeps saying arraydimension missing, then I call the method getDimension() and I inserted it in various ways

new int[getDimension()][] = TableAdapter.getTableC()

or

new int[getDimension()][new int[getDimension()][] = TableAdapter.getTableC()

Thanks in adavance, and I am using Netbeans.


I get the an animal table which has two types of animals and from this I interpret to integer code which is stored in a new table(tableC) just to make it easier

package tigers.bunnies;


public class TableAdapter {

static public int tableC[][];//=new int[3][3];
static private int dimension;

public void Table(){
    Animal tableT[][];
    tableT = table.getTable();

    dimension=tableT.length;

    //int tableC[][];
    tableC = new int[dimension][dimension];

    for(int i=0;i<dimension;i++){
        for(int j=0;j<dimension;j++){
            if(tableT[i][j]==null){
                tableC[i][j]=0000;
            }
            else if(tableT[i][j] instanceof tiger){
                tableC[i][j]=0001;
            }
            else if(tableT[i][j] instanceof tiger){
                tableC[i][j]=0002;
            }

    }
}
}

public static int[][] getTableC() {
    return tableC;
}

public static int getDimension() {
    return dimension;
}


}

also when I use

jTable1.setModel(new javax.swing.table.DefaultTableModel(
        TableAdapter.getTableC()
    ));

it has these errors: ]![错误 (C:\\Users\\user\\Desktop\\error.png)

Probably your TableAdapter.getTable() method returns an array of single dimension. Also you didn't supply the table header but I don't think it's the direct cause of the exception. You should call setModel this way:

Object[] header = {"Column1", "Column2..."};
jTable1.setModel(new javax.swing.table.
    DefaultTableModel(TableAdapter.getTableC(), header)

Your getTableC method is static but your Table method, which initializes the array, is not, resulting in returning an uninitialized array. make Table method static or remove static keyword from getTableC, tableC and dimmension and make Table method a constructor.

package tigers.bunnies;


public class TableAdapter {

public int tableC[][];//=new int[3][3];
private int dimension;

public TableAdapter(){
    Animal tableT[][];
    tableT = table.getTable();

    dimension=tableT.length;

    //int tableC[][];
    tableC = new int[dimension][dimension];

    for(int i=0;i<dimension;i++){
        for(int j=0;j<dimension;j++){
            if(tableT[i][j]==null){
                tableC[i][j]=0000;
            }
            else if(tableT[i][j] instanceof tiger){
                tableC[i][j]=0001;
            }
            else if(tableT[i][j] instanceof tiger){
                tableC[i][j]=0002;
            }

    }
}
}

public int[][] getTableC() {
    return tableC;
}

public int getDimension() {
    return dimension;
}

Also, an int array is not an Object array. Change it to Integer before passing to JTable model:

TableAdapter ta = new TableAdapter();
int[][] temp = ta.getTableC();
Integer[][] Result = new Integer[temp.length][temp[0].length];
for(int i = 0; i < temp.length; i++){
   for(int j = 0; j < temp[0].length; j++)
      result[i][j] = new Integer(temp[i][j]);
}
Object[] header = {"Column1", "Column2"};
jTable1.setModel(new javax.swing.table.
DefaultTableModel(result, header)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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