Solved

Jcombobox giving error count on table data

Posted on 2011-03-11
9
532 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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 500 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Server Faces parameter pass? 6 50
what is a "java.lang.System Property"   ? 20 67
Way to decrease size of apk file 9 69
type mismatch (Object[] to double[] 4 24
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

809 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