0

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: ]![Error(C:\Users\user\Desktop\error.png)

4
  • why r u doing this new int[][] = TableAdapter.getTableC() Commented Jun 29, 2013 at 10:00
  • it was new Object[][] by default and I turned it to new int[][] just to make more compatible the transfer from the method,but in any case the same problem appears. Commented Jun 29, 2013 at 10:05
  • did u try luke's answer? Commented Jun 29, 2013 at 10:06
  • yeap it says no suitable constructor found Commented Jun 29, 2013 at 10:10

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

but I have to keep my tableC static because then I have the error you cannot pass non static method() tableC90 cannot be referenced from a static context, so I only changed the method to a constructor
Only changing it to a constructor won't work. Change Table() to static if you want to use this class in static context.
1

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)

6 Comments

that's the method: public static int[][] getTableC() { return tableC; }
What's the type of tableC?
static public int tableC[][];
How is it initialized? Are you sure it's not something like this: tableC = new int[size][]? Using multidimensional arrays you have to supply the size for each dimmension.
Could you post the TableAdapter class?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.