Solved

Jcombobox giving error count on table data

Posted on 2011-03-11
9
539 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

623 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