The question is how can I update the JTextField in Class CreditDisplayFi eld when I hit the button in class CreditButton? I wrote in a test printing to console to make sure the "logic.addCredi ts();" in the class ButtonHandler is being done and that works but the "creditDisplay. update();" in the same area still shows 0 and does not update w/ the added amount of credits. This was originally a homework assignment that I completed in 3 very large classes and I was told to break it down to better understand OOP so I am trying to get it to work. Any suggestions on how to write it would be very helpful.
JoeMac
JoeMac
Code:
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CreditDisplayField extends JPanel{
private JPanel creditDisplay;
private JLabel credits;
private JTextField creditsLeft;
private SlotLogic logic = new SlotLogic();
CreditDisplayField(){
creditDisplay = new JPanel();
credits = new JLabel("Credits");
creditsLeft = new JTextField(6);
creditsLeft.setText("" + logic.getCredits());
creditDisplay.add(credits);
creditDisplay.add(creditsLeft);
this.setLayout(new FlowLayout());
this.add(creditDisplay);
}
public void update(){
creditsLeft.setText("" + logic.getCredits());
}
}
Code:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class CreditButton extends JPanel {
private JPanel creditButton;
private JButton credit;
private ButtonHandler addCredits = new ButtonHandler();
SlotLogic logic = new SlotLogic();
CreditDisplayField creditDisplay = new CreditDisplayField();
CreditButton(){
creditButton = new JPanel();
credit = new JButton("Add Credits");
creditButton.add(credit);
credit.addActionListener(addCredits);
this.setLayout(new FlowLayout());
this.add(creditButton);
}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
logic.addCredits();
creditDisplay.update();
}
}// inner class ButtonHandler
}