Jcombobox giving error count on table data

Posted on 2011-03-11
Last Modified: 2013-11-24

I am having problems with my code below, the code below shows a jComboBox being populated, when i select an item from this list it is added to the jTable below it.

There is alos code to check for duplicate entries ont he table. If a duplicate entry is found it should increase the qty column by one and not create a seperate entry.

This is where the problem comes in, when I press the back button on this screen and go to a different screen and then come back via same route as the first time, I get an incrementally different qty added to the table row/cell.

I have also included the code that populates the Round Details depending on Round Drop selected from table, for reference, but Im fairly certain the problem lies in the below code. The navigation is as follows...

To get to the below screen... Round Drop panel table of round drops) >> click on table row and taken to associated round details panel >> pressing the Till button takes user to screen with code below...

Test results:

First pass through below code using navigation above gives results as expected

Second pass gives an initial value of 2 (instead of one), and duplicate row increases qty by 2 instead of one

Third pass gives an initial value of 3 (instead of one), and duplicate row increases qty by 3 instead of one

Fourth pass gives an initial value of 4 (instead of one), and duplicate row increases qty by 4 instead of one

...and so on.

Any help, guidance on solution or a better design would be hugely appreciated.


/****************************Code sample *********************************************************/
    public void tillOperations(String sourceCall) {  
        final DefaultTableModel model = (DefaultTableModel)main.tillPanel.tblTillSale.getModel();
            if (main.tillPanel.cmbTillProdSelect.getItemCount() < 1) {
                  for (int d = 0; d < roundStockObj.length ; d++) {
        main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount() - 1);
        main.tillPanel.cmbTillProdSelect.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent f)
                int qty = 1;
                        for (int index = 0; index < 4; index++) {
                    addSelectedItem[index] = "";
                int row;
                selectedItem = null;
                        main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount() - 1);
                        selectedItem = main.tillPanel.cmbTillProdSelect.getSelectedItem();

                for (int d = 0; d < roundStockObj.length; d++) {
                    if (selectedItem.equals(roundStockObj[d].getDescription())) {    
                        addSelectedItem[0] = roundStockObj[d].getDescription();
                        addSelectedItem[1] = Integer.toString(qty);
                        addSelectedItem[2] = Double.toString(roundStockObj[d].getPrice()).trim();
                        addSelectedItem[3] = Double.toString(roundStockObj[d].getPrice()).trim();
                if(model.getRowCount() == 0) { //check if model is empty
                    model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
                else { //check if there is a duplicate row
                    int duplicateRow = -1;
                    for (row = 0 ; row < model.getRowCount(); row++) {
                        if(addSelectedItem[0].equals(main.tillPanel.tblTillSale.getModel().getValueAt(row,0))) {
                            duplicateRow = row;

                    if(duplicateRow == -1) { //if there is no duplicate row, append
                                    model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
                    else { //if there is a duplicate row, update
                        DecimalFormat fmtObj = new DecimalFormat("####0.00");
                        int currentValue = Integer.parseInt(main.tillPanel.tblTillSale.getValueAt(row, 1).toString().trim());
                        int newValue = currentValue + 1;
                        Integer newValueInt = new Integer(newValue);
                        model.setValueAt(newValueInt, row, 1);
                        double unitPrice = Double.parseDouble(main.tillPanel.tblTillSale.getValueAt(row, 2).toString().trim());
                        double newPrice = newValue * unitPrice;
                        Double newPriceDbl = new Double(newPrice);
                        main.tillPanel.tblTillSale.setValueAt(fmtObj.format(newPriceDbl), row, 3);
                main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount() - 1);
                for (int index = 0; index < 4; index++) {
                    addSelectedItem[index] = "";

//This code loads the specific Round Details, based on the selection form the round drops table

    public void displayRoundDropDetails() {
        DefaultTableModel model = (DefaultTableModel)main.selectRoundDropPanel.tblSelectRoundDrop.getModel();
        if (!loaded) {
        for (int d = 0; d < roundDropsData.length; d++) {
            if (roundDropsData[d][0].equals(defaultRoundID)) {
                model.addRow(new Object[]{roundDropsData[d][3], roundDropsData[d][2],
                        roundDropsData[d][4], roundDropsData[d][5]});
        loaded = true;
        main.selectRoundDropPanel.tblSelectRoundDrop.addMouseListener(new MouseAdapter()
            public void mouseClicked(MouseEvent evt)
                int row = 0;
                row = main.selectRoundDropPanel.tblSelectRoundDrop.getSelectedRow();
                for (int index = 0; index < roundDropsData.length; index++) {
                    if (roundDropsData[index][3].equals(
                            main.selectRoundDropPanel.tblSelectRoundDrop.getModel().getValueAt(row, 0))) {
                        main.roundDetailsPanel.txtRoundDetailsAddress.setText(roundDropsData[index][6] + "\n"
                                + roundDropsData[index][7] + ", " + roundDropsData[index][8] + "\n" +

            Globals.CURRENT_COMPONENT = "selectRoundDropPanel";
Question by:BinaryFlusher
  • 6
  • 2
LVL 86

Expert Comment

ID: 35107375
This is essentially the same as your previous q - why did you close it?

Author Comment

ID: 35107443
I found a solution to the duplicate rows not being displayed, but it now appears to be adding the values of the other rows without the actual row, if that makes sense.

I have sopent severl frustrating hours on this trying to resolve and have had no success...

Author Comment

ID: 35107929
Is it worth moving the bulk of the processing out of the actionPerformed(), so the click or selection registers the selectedItem and I use this selectedItem value for the processing outside of the inner class actionPerformed method?

Not able to test it as I'm at work, and ths is for a private piece of work...

Would using an ItemListestener work, instead of ActionListener, and registrer if the action is a selection or deselection and ignore one or the other? But then again, this doesnt explain why the first iteration works, somewhere something is being incremented by one each time...?
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 92

Expert Comment

ID: 35113073
yes I'd be breaking up the code, too hard to debug otherwise.

Author Comment

ID: 35116012
The issue I am having is that when I come back to the coimbobox and select another item this item is added to the table twice, so due the code for duplicates it supresses this and increases the qty by 2 instead of one.

If I remove the 'looking for duplicate' code and juyst add the row to the table it adds it twice.

Does anyone know why it would be selecting and displaying the row twice, and three times the third pass through? Am I suppsoed to clear the selection of the combo and table prior to the selection? If so how do I do this?

LVL 92

Accepted Solution

objects earned 500 total points
ID: 35118226
check how many times the listener is firing

Author Comment

ID: 35867924
I've requested that this question be closed as follows:

Accepted answer: 0 points for BinaryFlusher's comment http:/Q_26879806.html#35107929

for the following reason:

no suitable answers given

Author Comment

ID: 35867925

Author Closing Comment

ID: 35867926
No definitive answer given

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 57
reverse digits of a number using for loop 5 53
CSV file parsing thru Java 13 34
Java basic valueOf question 1 29
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question