0

I am trying to get the Decimal point to display with the click of an GUI button. I using the method below, which works fine for the numbers but not the decimal point. Any advise would be great. thanks

private void displayWeightedquantity(ActionEvent event){
    JButton currentButton = null;
    JButton[] numberButtonsarray = {bnDecimal,bnZero,bnOne, bnTwo, bnThree, bnFour, bnFive,bnSix,bnSeven,
                                    bnEight,bnNine};
        
    currentButton = (JButton)event.getSource();  
    for (int i = 0; i <numberButtonsarray.length; i++){
         if (currentButton == numberButtonsarray[i]){
             lbDisplayitemQty.setText(lbDisplayitemQty.getText() + currentButton.getText());  
             break;
             }
        } 
   }

My virtual numberpad :

enter image description here

Community
  • 1
  • 1
Adesh
  • 937
  • 4
  • 11
  • 17
  • 2
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Nov 11 '12 at 23:26
  • BTW - I suspect that `ActionEvent.getActionCommand()` will produce the character that most of that code is designed to determine. – Andrew Thompson Nov 11 '12 at 23:49
  • 3
    Why are you looping over the buttons? Instead you could have; `if (currentButton != bnPurcahse || currentButton != bnClear) lbDisplayitemQty.setText(lbDisplayitemQty.getText() + currentButton.getText());` This is a much more efficient way of implementing the same functionality. Anyways, your code looks good to me so I would assume you have some typo type of error with `bnDecimal` causing the `currentButton == numberButtonsarray[i]` to never be met for `.`. I don't see any errors in that logic so I would look elsewhere (or take the above advice :). – evanmcdonnal Nov 11 '12 at 23:51
  • @AndrewThompson I followed your advice even though I hate reading this with the comment formatting. – evanmcdonnal Nov 11 '12 at 23:52
  • 4
    I suspect the problem is else where in your code. The test I set up works find. You need to provide a functional example the shows the problem, not just a code snippet. Make sure that the decimal button has it's action listener attached – MadProgrammer Nov 11 '12 at 23:53
  • 2
    @evanmcdonnal I agree about reading code in comments. It is horrendous. OTOH we need to stick to the conventions of SO. If that means the OP gets code back in a form that is difficult to read, they just need to deal with it. – Andrew Thompson Nov 11 '12 at 23:55
  • @ Andrew and MadProgrammer :- The action listener was missing. I am just a student guys..thank you – Adesh Nov 12 '12 at 00:19
  • @MadProgrammer *"Make sure that the decimal button has it's action listener attached"* It seems your intuitive leap is correct. Care to enter that as an answer? – Andrew Thompson Nov 12 '12 at 00:40
  • @AndrewThompson It's amazing the number of times I've done that exact same thing :P – MadProgrammer Nov 12 '12 at 00:43
  • @MadProgrammer ..Do you have a crystal ball? ;) – Andrew Thompson Nov 12 '12 at 00:44
  • @AndrewThompson No, just years of stupid, silly little mistakes ;) – MadProgrammer Nov 12 '12 at 00:45

1 Answers1

1

I suspect the problem is else where in your code.

The test I set up works find.

You need to provide a functional example the shows the problem, not just a code snippet.

Also, make sure that the decimal button has it's action listener attached

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366