Solved

Jcombobox giving error count on table data

Posted on 2011-03-11
9
521 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
Comment Utility
This is essentially the same as your previous q - why did you close it?
0
 

Author Comment

by:BinaryFlusher
Comment Utility
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
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
yes I'd be breaking up the code, too hard to debug otherwise.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:BinaryFlusher
Comment Utility
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 500 total points
Comment Utility
check how many times the listener is firing
0
 

Author Comment

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

Author Closing Comment

by:BinaryFlusher
Comment Utility
No definitive answer given
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now