I am attempting to make a basic character creation planner in netbeans as an attempt to learn Java so forgive me if my question is a bit silly.
I have created the a text field that needs to be updated with a formula each time the relevant stat is changed.
In this example the luck and charisma stat affects the barter skill. So when I change either of these stats I need the formula to run again to update the barter skill.
Currently the formula runs on creation of the object but not on update of another stat.
Here is my current (relevant) code:
package AppPackage;
import javax.swing.JOptionPane;
public class StartGUI extends javax.swing.JFrame {
public StartGUI() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
BarterPts = new javax.swing.JTextField();
Chr = new javax.swing.JTextField();
ChrPlus = new javax.swing.JButton();
Luck = new javax.swing.JTextField();
BarterPts.setEditable(false);
BarterPts.setText("0");
BarterPts.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BarterPtsActionPerformed(evt);
}
});
getContentPane().add(BarterPts, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 110, 30, -1));
int a = Integer.parseInt(Chr.getText()),l = Integer.parseInt(Luck.getText()),barter;
barter = (2 + (a*2) + (l/2) );
BarterPts.setText(String.valueOf(barter));
//plus button to add to the Charisma stat.
private void ChrPlusActionPerformed(java.awt.event.ActionEvent evt) {
int a = Integer.parseInt(Chr.getText()),i,l = Integer.parseInt(SLeft.getText());
if (a == 10) {
//print error
JOptionPane.showMessageDialog(null, "The Charisma value cannot exceed 10.", "Error", JOptionPane.ERROR_MESSAGE);
}
else if (l == 0) {
JOptionPane.showMessageDialog(null, "You have no special points remaining.", "Error", JOptionPane.ERROR_MESSAGE);
}
else{
l=--l;
SLeft.setText(String.valueOf(l));
i=++a;
Chr.setText(String.valueOf(i));
}
}
private void BarterPtsActionPerformed(java.awt.event.ActionEvent evt) {
}
There are variable declarations below but I have not included them.
So, from your original question it looks like you are expecting an update to the text field based on a button click.
Currently, you have your ActionListener registered to your text field. Instead, your ActionListener should be registered to your button.
Registering the Listener to the button will force behavior on a click as expected.