Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Jcombobox giving error count on table data

Posted on 2011-03-11
9
Medium Priority
?
543 Views
Last Modified: 2013-11-24
Hi

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.

Thanks

/****************************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.cmbTillProdSelect.addItem(roundStockObj[d].getDescription());
            }}
        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();
                        //break;
                    }
                }
               
                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;
                            break;
                        }
                    }

                    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
                        main.tillPanel.jLabel1.setText(addSelectedItem[1]);
                        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" +
                                roundDropsData[index][9]);

                        main.roundDetailsPanel.lblRoundDetailsName.setText(roundDropsData[index][2]);
                        main.roundDetailsPanel.txtRoundDetailsInstuct.setText(roundDropsData[index][10]);
                        main.roundDetailsPanel.txtDropDetailsIn.setText(roundDropsData[index][4]);
                        main.roundDetailsPanel.txtDropDetailsOut.setText(roundDropsData[index][5]);
                        main.roundDetailsPanel.txtRoundDetailsInstruct.setText(roundDropsData[index][12]);
                        break;
                    }
                }
            Globals.CURRENT_COMPONENT = "selectRoundDropPanel";
            showRoundDetailsPanel();
            }
        });
    }
0
Comment
Question by:BinaryFlusher
  • 6
  • 2
9 Comments
 
LVL 86

Expert Comment

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

Author Comment

by:BinaryFlusher
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...
0
 

Author Comment

by:BinaryFlusher
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...?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

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

Author Comment

by:BinaryFlusher
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?

Thanks
0
 
LVL 92

Accepted Solution

by:
objects earned 1500 total points
ID: 35118226
check how many times the listener is firing
0
 

Author Comment

by:BinaryFlusher
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
0
 

Author Comment

by:BinaryFlusher
ID: 35867925
na
0
 

Author Closing Comment

by:BinaryFlusher
ID: 35867926
No definitive answer given
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

773 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