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 new NewClass() instead of using the calling instance of NewClass. Try passing instance of NewClass into Cart2 constructor.
For example:
public class Cart2 extends JFrame implements ActionListener {
JButton remove = new JButton("remove");
NewClass newClass;
public Cart2(NewClass newClass) {
this.newClass = newClass;
add(remove);
remove.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == remove) {
// NewClass nc = new NewClass();
newClass.update(1000);
}
}
}
Then in NewClass:
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == viewcart) {
Cart2 c = new Cart2(this);
c.setVisible(true);
c.setSize(250, 230);
}
}
don't use two or more JFrames, not on runtime, this is road to the troubles
this is job for CardLayout
put every JComponents to the JPanel, those JPanels put as Cards
(maybe) then is place for only one JButton for whole GUI
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With