I have my Store class wherein i could add items to cart then it will increment the cart item number and the total amount.
I also have a view cart button like thing, if it is clicked it will show another frame where the items from the cart shows. When i click remove button from this cart frame i plan to decrement the count and total amount from the previous frame, but the setText method i used in the jLabel where the total price is located don’t work.
I call this method from the cart frame then pass the price to be removed whenever i click the remove button
public void updateTotalAmount(double deduct){
System.out.println("updateTotalAmount - "+deduct);
tAPriceL.setText(String.valueOf(deduct)); //Total amount price label
cICountL.setText(String.valueOf(--cICount)); //cart item count label
}
the system.out line is the only statement that works, the rest don’t.
when i try to interchange the code like this.
public void updateTotalAmount(double deduct){
tAPriceL.setText(String.valueOf(deduct)); //Total amount price label
cICountL.setText(String.valueOf(--cICount)); //cart item count label
System.out.println("updateTotalAmount - "+deduct);
}
the system.out now didn’t work so i guess there is a problem on the setText part.
I can’t figure out where is the problem.
Could anyone help me with this?
here is the summary of this..
for the main store class.. example i have 5000 worth of items
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class NewClass extends JFrame implements ActionListener {
JLabel tAPrice = new JLabel("5000");
JButton viewcart = new JButton("view cart");
public NewClass() {
this.setLayout(new FlowLayout());
add(tAPrice);
add(viewcart);
viewcart.addActionListener(this);
}
public static void main(String[] args) {
NewClass n = new NewClass();
n.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
n.setSize(1150, 730);
n.setVisible(true);
}
public void update(double deduct) {
System.out.println("updated");
tAPrice.setText(String.valueOf(Double.parseDouble(tAPrice.getText())
- deduct));
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == viewcart) {
Cart2 c = new Cart2();
c.setVisible(true);
c.setSize(250, 230);
}
}
}
and for the cart class… for example i wanna remove 1000 from the total amount
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Cart2 extends JFrame implements ActionListener {
JButton remove = new JButton("remove");
public Cart2() {
add(remove);
remove.addActionListener(this);
}
public static void main(String[] args) {
Cart2 r = new Cart2();
r.setVisible(true);
r.setSize(250, 230);
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == remove) {
NewClass nc = new NewClass();
nc.update(1000);
}
}
}
In
Cart2.actionPerformed()you’re allocating newNewClass()instead of using the calling instance ofNewClass. Try passing instance ofNewClassintoCart2constructor.For example:
Then in
NewClass: