0

I have two JFrame in my GUI.

One is order frame and another is payment frame.

I would like to add onto the quantity variable every time when the user clicked on the button. I have added the increment-logic under the button method, but it seems only applicable in that method only. It won't add +1 into my array variable.

Here is my code for this:

class Bun extends javax.swing.JFrame {
 String decimal = "0.00";
 DecimalFormat df = new DecimalFormat(decimal);
 public final String [] bname = new String [] {"Tuna Sandwich","Garlic Bread","Redbean Bun"};
 public final double [] bprice = new double [] {1.20, 1.50, 1.50};
 public double [] bsub = new double [9];
 public int [] bquantity = new int[]{0,0,0};
 public double bsubtotal;

private void BtnTunaSandwichActionPerformed(java.awt.event.ActionEvent evt) {                                                
    bquantity[0]++;
    numTunaSandwich.setText(Integer.toString(bquantity[0]));
}                                               

private void BtnGarlicBreadActionPerformed(java.awt.event.ActionEvent evt) {                                               
   bquantity[1]++;
   numGarlicBread.setText(Integer.toString(bquantity[1]));
}                                              

private void BtnRedbeanBunActionPerformed(java.awt.event.ActionEvent evt) {                                              
   bquantity[2]++;
   numRedbeanBun.setText(Integer.toString(bquantity[2]));
}                      

public void calculatesub()
{
    for(int counter=0;counter<bsub.length;counter++)
    {
        bsub[counter]=bquantity[counter]*bprice[counter];
    }
}
public void setsubtotal()
{
    for(int counter=0;counter<bsub.length;counter++)
    {
        bsubtotal += bsub[counter];
    }
}
public String getsubtotal()
{
    return (df.format(bsubtotal));
}

This is my Payment frame code:

private void subtotalActionPerformed(java.awt.event.ActionEvent evt) {                                         
        Bun bun = new Bun();
        bun.calculatesub();
        bun.setsubtotal();
        subtotal.setText(bun.getsubtotal());
}      

Whenever I run the program and try to calculate the subtotal, it shows 0.

My interface:

Order Frame Payment Frame

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Silver Archer
  • 167
  • 3
  • 3
  • 14
  • 1
    Where do you call `calculatesub` ? – Arnaud Apr 21 '16 at 12:22
  • I think you might need to change the `bquantity[0]++` to `bquantity[0] = bquantity[0] + 1` - I think `++` means increase it by 1 and return the old value, but `bquantity[0] = bquantity[0] + 1` means increase it by 1 and return the new value. – Thraydor Apr 21 '16 at 12:28
  • You need to hold the `bquantity` object in a single class. Maybe both JFrames have there own objects? Probably there are two `=new int[3];` – Joop Eggen Apr 21 '16 at 12:29
  • @Berger I call it in my another frame – Silver Archer Apr 21 '16 at 12:32
  • What @Berger said: where do you call `calculatesub()` and `setsubtotal()`? You have the methods but I don't see you actually **using** them anywhere. Also from what I can see `calculatesub()` will go out of bounds. – MichaelK Apr 21 '16 at 12:32
  • @MichaelKarnerfors I call them in my Payment frame, that's what other people told me for pass the value. – Silver Archer Apr 21 '16 at 12:33
  • @JoopEggen No I didn't declare anything in another frame – Silver Archer Apr 21 '16 at 12:33
  • @Thraydor It makes sense but its not working... – Silver Archer Apr 21 '16 at 12:36
  • Two problems in your code: bsub should be 3 elements long, not 9. And in `setsubtotal()` you are not resetting `bsubtotal` to 0 before you add to it. – MichaelK Apr 21 '16 at 12:38
  • @MichaelKarnerfors The bsub is actually my mistake in this post, there are actually 9 arrays in my code. But that's too long so I cut short for the post. I have test the method in another class and its working. So it must be the bquantity problem... – Silver Archer Apr 21 '16 at 12:46
  • @SilverArcher Well... we need to see how you set up the two frames then because it is obviously there that your problem lies. Also, I am noticing that your action listeners are `private` which means that the other frame cannot be calling them. Append your code to show up how you set up the two frames and pass a reference of this one ("Bun") to the other. – MichaelK Apr 21 '16 at 12:49
  • @MichaelKarnerfors Edited and add my interface, thanks – Silver Archer Apr 21 '16 at 12:53
  • @SilverArcher Ok, you cannot make a **new** `Bun` every time you press the subtotal button. Because then you have two instances of `Bun`; the one that is already showing, and the new one. You are asking for the subtotal from the new one (that is invisible because you have not set it to visible). You need to pass a reference of the previously created `Bun` and use **that** in your `subtotalActionPerformed`. – MichaelK Apr 21 '16 at 12:58
  • @SilverArcher It might seem simple, but have you added the action listener to your buttons for the menu selections? – Thraydor Apr 21 '16 at 12:59
  • @MichaelKarnerfors But I thought the Bun bun = new Bun is the object ? For let this class have access to another class. – Silver Archer Apr 21 '16 at 13:04
  • @Thraydor Is it the Event -> Action -> ActionPerformed ? I have done that to every button and write the bquantity[arraynumber]++ under it. – Silver Archer Apr 21 '16 at 13:05
  • @SilverArcher Well we can't see your declared buttons/fields etc. So you have done `BtnTunaSandwich.addActionListener(...)` for the buttons? – Thraydor Apr 21 '16 at 13:08
  • @SilverArcher "Bun bun = new Bun is the object ?". Yes, that is your problem. You do **not** want to call methods on a **new** object, you want to call it on the old one that already exists and that is showing on your screen! :) – MichaelK Apr 21 '16 at 13:10
  • @Thraydor Uh, I think not. Just this one: private void BtnTunaSandwichActionPerformed(java.awt.event.ActionEvent evt) { bquantity[0]++; numTunaSandwich.setText(Integer.toString(bquantity[0])); } – Silver Archer Apr 21 '16 at 13:11
  • @SilverArcher Have a read of this - http://stackoverflow.com/questions/284899/how-do-you-add-an-actionlistener-onto-a-jbutton-in-java - you need to add listeners to the buttons. They need a link. – Thraydor Apr 21 '16 at 13:12
  • @MichaelKarnerfors Okay but after I delete this line it gives me cannot find symbol error on bun.calcualtesub, bun.setsubtotal and bun.getsubtotal. – Silver Archer Apr 21 '16 at 13:13
  • @SilverArcher ...which is why I said you need to pass a reference of your previously created `Bun` object to your Payment Frame. – MichaelK Apr 21 '16 at 13:14
  • @MichaelKarnerfors Is it okay if I write something like this in the Payment frame ? Bun bun; public void setBun(Bun bun) { this.bun = bun; } – Silver Archer Apr 21 '16 at 13:28
  • That should do it, yes. So then you do new Bun, new Payment, payment.setBun (bun). – MichaelK Apr 21 '16 at 13:30
  • @MichaelKarnerfors I wrote new bun, new payment and pay.setBun(bun) in the Order Frame, right ? Then should I call that method in Payment frame ? – Silver Archer Apr 21 '16 at 13:34
  • @SilverArcher How are the listeners coming along? Without a listener attached to the button there won't be any code that is activated when you click it – Thraydor Apr 21 '16 at 13:42
  • @Thraydor I'm learning it from some youtube video and some post, its a new thing to me haha, thanks for your help ! – Silver Archer Apr 21 '16 at 13:44
  • @SilverArcher You're welcome. Once you have the action listener in for one of the food buttons, it's easy enough to test it works just by keep clicking the button, and then you can just copy the code across to the other buttons for their listeners. There are multiple ways to do it (as shown on the link I gave), you just need to decide which suits your program better. – Thraydor Apr 21 '16 at 13:48
  • @SilverArcher This has some information on buttons and using listeners also - https://docs.oracle.com/javase/tutorial/uiswing/components/button.html#abstractbutton – Thraydor Apr 21 '16 at 13:52

2 Answers2

0

You need to have field in your class like

public int qvantity=0;

And increse this every time wheb someone click on button and you need again to set text on jlabel if you use it for represent quvantity all that you need to have on method for click button

Update: Or you can create one method which will increse qvantity and set text to label which you use. And this method you need to call in actionPerformed

Update 2: Did you try to setText of subtotal textField when you click on some of button. You can try with some method which will calculate all order, and then call that method on payment button, that method will return some int and that int you need to use in setText of subtotal textField, I realy don't know why you use txtField for it.

GlacialMan
  • 579
  • 2
  • 5
  • 20
0

change your bsub length to 3 instead of 9.

ERJ
  • 9
  • 7