Dunkey
Dunkey

Reputation: 1922

Display multiple arrays to JTable

I'm doing a very basic Java GUI app. but I don't know what I'm doing wrong in here.

This is what I have done so far:

 String[] colNames = { "QTY", "Item Code", "Amount" };

 model = new DefaultTableModel(colNames, 0);  
 JTable table = new JTable(model);

 String[] arrQty = {"2", "3", "10"};
 String[] arrItemCode = {"Item 1", "Item 2", "Item 3"};
 String[] arrAmount = {"100", "200", "80"};
 String f_qty, f_itemcode, f_amount;    

 for(String arrAmt: arrAmount){
    f_amount = arrAmt;
 }

 for(String arrQt : arrQty){
    f_qty = arrQt;
 }

 for(String arrItem : arrItemCode){
    f_itemcode = arrItem;
 }

 model.addRow(new Object[]{ f_qty, f_itemcode, f_amount });

But it won't allow me because compiler needs to initialize first the strings, how can I fix this issue? Any ideas?

Upvotes: 0

Views: 200

Answers (5)

René Link
René Link

Reputation: 51333

In order to loop over the values of all arrays the arrays must have the same length or you might get an ArrayIndexOutOfBoundsException or miss some values.

Thus I would first introduce a check that makes it easier to find a problem in different array length.

String[] arrQty = { "2", "3", "10" };
String[] arrItemCode = { "Item 1", "Item 2", "Item 3" };
String[] arrAmount = { "100", "200", "80" };

if (arrQty.length != arrItemCode.length || arrQty.length != arrAmount.length) {
    throw new IllegalStateException("All arrays must have the same length." 
                                  + "arrQty[" + arrQty.length 
                                  + "], arritemCode[" + arrItemCode.length 
                                  + "], arrAmount[" + arrAmount.length + "]");
}

after that you can safely loop over the string arrays and add the rows to the table model.

for (int i = 0; i < arrQty.length; i++) {
    model.addRow(new Object[] { arrQty[i], arrItemCode[i], arrAmount[i]});
}

Upvotes: 1

Rahul
Rahul

Reputation: 3509

Use as below:

Create a initial empty 2D array of object:

Object[ ][ ] rowData = { };

Create Datamodel as below:

model = new DefaultTableModel(rowData,colNames);

Fill the table as your need.

Upvotes: 0

Qadir Hussain
Qadir Hussain

Reputation: 1263

Initialize your String variables with null.

Upvotes: 0

alex2410
alex2410

Reputation: 10994

For initializing this variables you can use next line:

String f_qty="", f_itemcode="", f_amount="";    

But seems for me, you just need to do next:

DefaultTableModel model = new DefaultTableModel(colNames, 0);  
JTable table = new JTable(model);

String[] arrQty = {"2", "3", "10"};
String[] arrItemCode = {"Item 1", "Item 2", "Item 3"};
String[] arrAmount = {"100", "200", "80"};

for(int i=0;i<arrQty.length;i++){
    model.addRow(new Object[]{ arrQty[i], arrItemCode[i], arrAmount[i] });
}

and result will be:

enter image description here

Upvotes: 2

sanbhat
sanbhat

Reputation: 17622

Fix is initializing String with null OR empty string or any string

String f_qty= null, f_itemcode =null, f_amount =null;    

Second point is, your logic seems to be wrong. You are setting the value to the table after iteration is finished. Should it be?

for(int i=0; i< arrQty.length; i++) {
    model.addRow(new Object[]{ arrQty[i], arrItemCode[i], arrAmount[i});
}

Upvotes: 2

Related Questions