BinaryFlusher
asked on
Checking for duplicate rows in jTable data
Hi,
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:
1. First row is added to table, with a value of "200" (should be "100" as table was empty prior to this selection).
2. 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.
3. Third selection from combo does as in 2 above, so value is correct but should only be one row
4. 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.ti llPanel.tb lTillSale. getModel() ;
//populate the combo box
for (int d = 0; d < roundStockObj.length ; d++) {
main.tillPanel.cmbTillProd Select.add Item(round StockObj[d ].getDescr iption());
}
//add selection listener to combo
main.tillPanel.cmbTillProd Select.add ItemListen er(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
String[] addSelectedItem = new String[4];
selectedItem = main.tillPanel.cmbTillProd Select.get SelectedIt em();
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].getDescr iption().e quals(sele ctedItem)) {
addSelectedItem[0] = roundStockObj[d].getDescri ption();
addSelectedItem[2] = Double.toString(roundStock Obj[d].get Price()).t rim();
addSelectedItem[3] = Double.toString(roundStock Obj[d].get Price()).t rim();
}
}
main.tillPanel.tblTillSale .removeRow SelectionI nterval(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.tblTillSa le.getMode l().setVal ueAt(selec tedItem, 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].equa ls(main.ti llPanel.tb lTillSale. getValueAt (rowCount, 0))) {
main.tillPanel.lblTotPrice .setText(" 200");
int currentValue = Integer.parseInt(addSelect edItem[1]. trim());
addSelectedItem[1] = "200";
model.setValueAt(addSelect edItem[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;
}
}
});
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:
1. First row is added to table, with a value of "200" (should be "100" as table was empty prior to this selection).
2. 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.
3. Third selection from combo does as in 2 above, so value is correct but should only be one row
4. 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.ti
//populate the combo box
for (int d = 0; d < roundStockObj.length ; d++) {
main.tillPanel.cmbTillProd
}
//add selection listener to combo
main.tillPanel.cmbTillProd
{
public void itemStateChanged(ItemEvent
{
String[] addSelectedItem = new String[4];
selectedItem = main.tillPanel.cmbTillProd
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].getDescr
addSelectedItem[0] = roundStockObj[d].getDescri
addSelectedItem[2] = Double.toString(roundStock
addSelectedItem[3] = Double.toString(roundStock
}
}
main.tillPanel.tblTillSale
//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
//main.tillPanel.tblTillSa
main.tillPanel.lblTotPrice
break;
}
// look for duplicate row and if found increase total column of existing row, and not add this selection
if(addSelectedItem[0].equa
main.tillPanel.lblTotPrice
int currentValue = Integer.parseInt(addSelect
addSelectedItem[1] = "200";
model.setValueAt(addSelect
break;
}
//if no duplicate found add this row to the table
else {
addSelectedItem[1] = "300";
model.addRow(new String[]{addSelectedItem[0
main.tillPanel.lblTotPrice
break;
}
}
//clear the current selection array of row data
for (int index = 0; index < 4; index++) {
ddSelectedItem[index] = null;
}
}
});
I would certainly recommend inserting some System.out debug prints in there
ASKER
Unfortunatley I am using a JVM for a Win CE device (CreMe), and this does not allow me to use the in-built debug functions in Netbeans...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
objects
Not sure what you mean by include the object in the combo model...
culd you give me a little more detail, and in terms of renedering, is this to do with decoration or only displaying a specific part of the data?
Also you'll make your code a lot simpler my storing the actual objects (instead of just the description) in the combo model. And use a custom renderer to render it.
Not sure what you mean by include the object in the combo model...
culd you give me a little more detail, and in terms of renedering, is this to do with decoration or only displaying a specific part of the data?
There's generally no need to use a custom renderer actually. If you do want to store the objects in your model, just ensure that the class in question has a toString method that returns what you'd like displayed in the combo.
If you attach your stock object source code (as a .txt file) then we can probably advise more specifically.
Did the comment at http:#35074396 fix the problems?
If you attach your stock object source code (as a .txt file) then we can probably advise more specifically.
Did the comment at http:#35074396 fix the problems?
> Not sure what you mean by include the object in the combo model...
you would add the following to your model
for (int d = 0; d < roundStockObj.length ; d++) {
main.tillPanel.cmbTillProd Select.add Item(round StockObj[d ]);
}
Then use a custom render to just display the description
Never use toString(), thats a common trap inexperienced developers often fall into. Looks simpler but will just cause you problems.
public class IconListRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererCompone nt(
JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
StockClass bean = (StockClass) value;
return super.getListCellRendererC omponent(l ist, bean.getDescription(), index, isSelected, cellHasFocus);
}
}
you would add the following to your model
for (int d = 0; d < roundStockObj.length ; d++) {
main.tillPanel.cmbTillProd
}
Then use a custom render to just display the description
Never use toString(), thats a common trap inexperienced developers often fall into. Looks simpler but will just cause you problems.
public class IconListRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererCompone
JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
StockClass bean = (StockClass) value;
return super.getListCellRendererC
}
}
>>... but will just cause you problems.
It won't cause you any problems ;)
It won't cause you any problems ;)
ASKER
thanks for your help guys, using the above comments has helped me find a solution.
One thing, I am working on a Win CE6 device using a Creme JVM and the version of javba this uses (a version of Java ME CDC) wont allow the use of @override.
Thanks for your help.
One thing, I am working on a Win CE6 device using a Creme JVM and the version of javba this uses (a version of Java ME CDC) wont allow the use of @override.
Thanks for your help.
good to hear. Please close the question by accepting my comment if everything is fixed.
And let me know if you have any problems :)
And let me know if you have any problems :)