0

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?

5 Answers 5

2

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

2 Comments

+1 removing my answer, this would suffice per OPs requirement :)
@sanbhat thanks. but let me ask is this efficient? If arrqty has only 2 elements while other arrays have more than 2 elements?
2

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

2 Comments

thank you! This solves my problem. But is this method efficient? I mean what if the qty has only 2 elements and the other arrays have 5 elements?
I think you need to manage that by yourself, because only you know what need to display if arrays have different length.
1

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]});
}

Comments

0

Initialize your String variables with null.

Comments

0

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.

Comments

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.