Reputation: 91
Below is code I have written to to help do the following:
A selection is made from a jComboBox to be added to a jTable, it is then checked to see which one of the three conditions it meets below: if table is empty, it is added. If the item selected from the combo box exists in the table, the existing row has its qty value increased by one. Or if its not already on the table it is added.
But what I am geting is very strange behaviour as follows:
First row is added to table, with a value of "200" (should be "100" as table was empty prior to this selection).
A second item is selected form the combo box, the first row (above) has its value changed to "100" (this should happen on first selection). Also the second item selected from the combo box is added to the table twice (two identical rows) with a value of "300", value is correct but should only be one row.
Third selection from combo does as in 2 above, so value is correct but should only be one row
A fourth selection from the combo box is selected, this time to match an exisiting row in the table, instead of updating the value in the existing row it also adds two rows with a value of "300"...
I think maybe I have the loops wrong, but I am now well behind on a project trying to resolve this, so any help owould be greatly received...
Thanks in advance
final DefaultTableModel model = (DefaultTableModel)main.tillPanel.tblTillSale.getModel();
//populate the combo box
for (int d = 0; d < roundStockObj.length ; d++) {
main.tillPanel.cmbTillProdSelect.addItem(roundStockObj[d].getDescription());
}
//add selection listener to combo
main.tillPanel.cmbTillProdSelect.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
String[] addSelectedItem = new String[4];
selectedItem = main.tillPanel.cmbTillProdSelect.getSelectedItem();
for (int d = 0; d < roundStockObj.length; d++) {
//when selction form combo is matched, an array is created to hold the row data
if (roundStockObj[d].getDescription().equals(selectedItem)) {
addSelectedItem[0] = roundStockObj[d].getDescription();
addSelectedItem[2] = Double.toString(roundStockObj[d].getPrice()).trim();
addSelectedItem[3] = Double.toString(roundStockObj[d].getPrice()).trim();
}
}
main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount());
//if table is empty
for (int rowCount = 0 ; rowCount <= model.getRowCount(); rowCount++) {
if (model.getRowCount() == 0 ) {
addSelectedItem[1] = "100";
model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
//main.tillPanel.tblTillSale.getModel().setValueAt(selectedItem, tillSelectedRow, tillSelectedRow);
main.tillPanel.lblTotPrice.setText("100");
break;
}
// look for duplicate row and if found increase total column of existing row, and not add this selection
if(addSelectedItem[0].equals(main.tillPanel.tblTillSale.getValueAt(rowCount, 0))) {
main.tillPanel.lblTotPrice.setText("200");
int currentValue = Integer.parseInt(addSelectedItem[1].trim());
addSelectedItem[1] = "200";
model.setValueAt(addSelectedItem[1], rowCount, 1);
break;
}
//if no duplicate found add this row to the table
else {
addSelectedItem[1] = "300";
model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
main.tillPanel.lblTotPrice.setText("300");
break;
}
}
//clear the current selection array of row data
for (int index = 0; index < 4; index++) {
ddSelectedItem[index] = null;
}
}
});
Upvotes: 0
Views: 5431
Reputation: 1016
Looking at the code I strongly suspect that the problem is the breaks, they are turning your for loop into an if-else setup. They are the reason that you are only looking at the first row, I suspect that you actually meant to use continues rather than breaks(except for the case where the table is empty).
Assuming the contents of the blocks is correct, I think this should do what you are after:
if (model.getRowCount() == 0 ) {
//if table is empty, just add
addSelectedItem[1] = "100";
model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
//main.tillPanel.tblTillSale.getModel().setValueAt(selectedItem, tillSelectedRow, tillSelectedRow);
main.tillPanel.lblTotPrice.setText("100");
}else {
//table not empty, look for duplicates first
boolean found = false;
for (int rowCount = 0 ; rowCount < model.getRowCount(); rowCount++) {
// look for duplicate row
if(addSelectedItem[0].equals(main.tillPanel.tblTillSale.getValueAt(rowCount, 0))) {
//found the item : increase total column of existing row
found = true;
main.tillPanel.lblTotPrice.setText("200");
int currentValue = Integer.parseInt(addSelectedItem[1].trim());
addSelectedItem[1] = "200";
model.setValueAt(addSelectedItem[1], rowCount, 1);
}else {//not this row
}
}
if( found == false) {
//checked all rows without finding it : add this selection
addSelectedItem[1] = "300";
model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
main.tillPanel.lblTotPrice.setText("300");
} else {
}
}
Also (without knowing the context), looking at the lines:
int currentValue = Integer.parseInt(addSelectedItem[1].trim()); addSelectedItem[1] = "200";
is this meant to be:
int currentValue = Integer.parseInt(addSelectedItem[1].trim()); addSelectedItem[1] = "" + (currentValue+100);
?
At the moment this value you parsed is discarded at the end of the block.
Also note:
public static int parseInt(String s) throws NumberFormatException
You'll need to include try/catch blocks around any parsing
Upvotes: 1
Reputation: 88707
for (int rowCount = 0 ; rowCount <= model.getRowCount(); rowCount++)
I guess you mean
for (int rowCount = 0 ; rowCount < model.getRowCount(); rowCount++)
Otherwise you'd get an IndexOutOfBoundException in the last iteration (when rowCount == model.getRowCount()
).
Edit:
// look for duplicate row and if found increase total column of existing row, and not add this selection
You don't look for a duplicate row but check if the first row is a duplicate. If not you add the same item again (with "300").
This would be better:
//model is empty
if(model.getRowCount() == 0) {
//add first (case "100")
}
//model is not empty
else {
//check if there is a duplicate row
int duplicateRow = -1;
for (int row = 0 ; row < model.getRowCount(); row++) {
if(addSelectedItem[0].equals(main.tillPanel.tblTillSale.getValueAt(row,0))) {
duplicateRow = row;
break;
}
}
//if there is no duplicate row, append
if(duplicateRow == -1) {
//append (case "300")
}
//if there is a duplicate row, update
else {
//update row with index duplicateRow (case "200")
}
}
Upvotes: 0
Reputation: 23629
Edit: Wait, why do you have break statements inside each if or else. Your loop is only going to run once. So if it doesn't find the value the the first pass it will exit.
Take the break statements out. Also move the if statement to check if the table is empty to outside of the loop.
Upvotes: 0